快问,我想在启动没有返回值的异步任务之前等待一秒钟.
这是正确的方法吗?
Task.Delay(1000)
.ContinueWith(t => _mq.Send(message))
.Start();
Run Code Online (Sandbox Code Playgroud)
异常会发生什么?
在下面的片段中,SynchronizationContext丢失了,因为那也是CurrentCulture和CurrentUICulture.Log()来自这个答案.
public async Task<ActionResult> Index()
{
Log("before GetAsync");
await new HttpClient().GetAsync("http://www.example.com/")
.ContinueWith(request =>
{
Log("ContinueWith");
request.Result.EnsureSuccessStatusCode();
}, TaskContinuationOptions.AttachedToParent);
return View();
}
static void Log(string message)
{
var ctx = System.Threading.SynchronizationContext.Current;
System.Diagnostics.Debug.Print("{0}; thread: {1}, context: {2}, culture: {3}, uiculture: {4}",
message,
System.Threading.Thread.CurrentThread.ManagedThreadId,
ctx != null ? ctx.GetType().Name : String.Empty,
System.Threading.Thread.CurrentThread.CurrentCulture.Name,
System.Threading.Thread.CurrentThread.CurrentUICulture.Name);
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
在GetAsync之前; thread:56,context:AspNetSynchronizationContext,culture:nl,uiculture:nl
ContinueWith; 线程:46,上下文:,文化:nl-BE,uiculture:en-US
在此之前GetAsync,文化和UI文化具有我设定的价值观Application_BeginRequest.在内部ContinueWith,缺少上下文,文化被设置为浏览器提供的内容,并且UI文化被设置为某些默认值.
根据我的理解,一切都AspNetSynchronizationContext应该自动发生.我的代码出了什么问题?