如何引用我的代码在其中执行的任务?
ISomeInterface impl = new SomeImplementation();
Task.Factory.StartNew(() => impl.MethodFromSomeInterface(), new MyState());
...
void MethodFromSomeInterface()
{
Task currentTask = Task.GetCurrentTask(); // No such method?
MyState state = (MyState) currentTask.AsyncState();
}
Run Code Online (Sandbox Code Playgroud)
因为我正在调用一些接口方法,所以我不能只将新创建的任务作为附加参数传递.
等效的asp.net mvc 4.0代码是什么样的?
using System.Net;
using System.Net.Http;
using System.Web.Mvc;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Web.Controllers
{
public class HomeController : Controller
{
private HttpClient httpClient = new HttpClient();
private static dynamic shots;
public async Task<ActionResult> Index()
{
if (shots == null)
{
try
{
var responseMessage =
await httpClient.GetAsync
("http://api.dribbble.com/shots/everyone?per_page=30");
responseMessage.EnsureSuccessStatusCode();
var value = await responseMessage.Content.ReadAsStringAsync();
shots = await JsonConvert.DeserializeObjectAsync<dynamic>(value);
}
catch (WebException)
{
}
}
return View(shots);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道使用链接到一个或多个ActionBlocks的BufferBlock是否有好处,除了限制(使用BoundedCapacity),而不是直接发布到ActionBlock(只要不需要限制).
我有一个返回的对象System.Threading.Tasks.Task:
public class MyClass
{
public Task GetTask(object state, CancellationToken cancellationToken)
{
return new Task(Execute, state, cancellationToken);
}
public void Execute(object context)
{
//do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
在其他地方,我有一个List<MyClass>,所以我做以下来得到一个List<Task>:
var myTaskList = myClassList.Select(p => p.GetTask(null, cancellationToken)).ToList();
Run Code Online (Sandbox Code Playgroud)
现在我有了,我List<Task>怎么能并行启动它们?是否有更简洁的方法来编码?
谢谢!
我有一个WebService,它创建一个任务和一个延续任务.
在第一个任务中我们设置Thread.CurrentPrincipal
因此,当ContinuationTask启动时,它不再具有Thread.CurrentPrincipal.
我想在ContinuationTask中指定它应该在与其前提相同的线程中运行.
我搜索过网络,但我只发现线程要在SynchronizationContext中运行,因此我开始认为我缺少一些基本规则,特别是关于Thread.Principal应该如何工作.
以下代码将永远冻结.
public async Task DoSomethingAsync()
{
await Task.Delay(2000);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
DoSomethingAsync().Wait();
// Task.Delay(2000).Wait();
}
Run Code Online (Sandbox Code Playgroud)
如果我DoSomethingAsync使用注释掉的代码切换调用,它的行为与预期一致.我怀疑嵌套等待导致死锁,但我不知道为什么,或者如何解决它.
我不确定这是否可行,所以我在这里:
我有一系列动作来执行多个
async Task MethodA(...)
{
// some code
// a call to specific Async IO bound method
// some code
}
Run Code Online (Sandbox Code Playgroud)
还有MethodB(),MethodC()等,以及所有的具有完全相同的代码,除了呼叫到特定的异步IO绑定的方法.我试图找到一种方法将任务指针传递给方法,以便我们稍后可以在Method()中执行它.
我目前正在做的是:
private async Task Method(Func<Task<Entity>> func)
{
// some code
var a = await task.Run(func);
// some code
}
var task = async () => await CallToIOBoundTask(params);
Method(task);
Run Code Online (Sandbox Code Playgroud)
但是,此代码每次都会提取一个新线程,这对于IO绑定任务不是必需的,应该避免使用.
那么,有没有办法重构代码,以便不使用ThreadPool线程?目标是拥有这样的代码:
private async Task Method(Task<Entity> task)
{
// some code
var a = await task;
// some code
}
Run Code Online (Sandbox Code Playgroud)
同样重要的是要提到不同的IO调用具有不同的方法签名.此外,任务可以开始仅在Method()正文中执行,而不是之前执行.
我很难理解以下测试不起作用的原因:
[Fact]
public void repro()
{
var scheduler = new TestScheduler();
var count = 0;
// this observable is a simplification of the system under test
// I've just included it directly in the test for clarity
// in reality it is NOT accessible from the test code - it is
// an implementation detail of the system under test
// but by passing in a TestScheduler to the sut, the test code
// can theoretically control the execution of …Run Code Online (Sandbox Code Playgroud) 这是一个非常奇怪的情况,首先是代码......
代码
private List<DispatchInvoiceCTNDataModel> WorksheetToDataTableForInvoiceCTN(ExcelWorksheet excelWorksheet, int month, int year)
{
int totalRows = excelWorksheet.Dimension.End.Row;
int totalCols = excelWorksheet.Dimension.End.Column;
DataTable dt = new DataTable(excelWorksheet.Name);
// for (int i = 1; i <= totalRows; i++)
Parallel.For(1, totalRows + 1, (i) =>
{
DataRow dr = null;
if (i > 1)
{
dr = dt.Rows.Add();
}
for (int j = 1; j <= totalCols; j++)
{
if (i == 1)
{
var colName = excelWorksheet.Cells[i, j].Value.ToString().Replace(" ", String.Empty);
lock (lockObject)
{
if …Run Code Online (Sandbox Code Playgroud) 我已阅读TPL和任务库文档封面.但是,我仍然无法清楚地理解以下案例,现在我需要实施它.
我会简化我的情况.我IEnumerable<Uri>的长度是1000.我必须使用它来请求它们HttpClient.
我有两个问题.
Parallel.Foreach()吗?Task创建大量的最佳实践是什么?假设我使用Task.Factory.StartNew()并将这些任务添加到列表中并等待所有这些任务.是否有一个功能(如TPL分区程序)控制最大任务的数量和HttpClient我可以创建的最大值?在SO上有几个类似的问题,但没有人提到最大值.该要求仅使用具有最大HttpClient的最大任务.
先感谢您.