你能用自己的话解释STA和MTA吗?
什么是公寓线程,它们只与COM有关吗?如果是这样,为什么?
试图使用新的C#5异步模型,令我惊讶的AspNetSynchronizationContext是内部类(以及AspNetSynchronizationContextBase基础).因而无证.但是,在ASP.NET代码中使用async/await功能时,了解它的作用至关重要.我是否正确,它确实保证您的延续将与HttpContext.Current原始呼叫者相同?它不保证continuation将在与调用者相同的线程上执行吗?
如果后一个假设不正确并且我得到原始线程,我可以确保在continuation中获得相同的线程上下文吗?我的意思是与线程和线程本地存储相关的主体/文化?这很重要,因为ASP.NET本地化依赖于线程的文化,而我的应用程序依赖于.NET角色安全模型(线程的主体).
考虑以下:
private async void btnSlowPoke_Click(object sender, EventArgs e)
{
await DoItAsync();
}
private async Task<int> SomeLongJobAsync()
{
for (int x = 0; x < 999999; x++)
{
//ponder my existence for one second
await Task.Delay(1000);
}
return 42;
}
public async Task<int> DoItAsync()
{
Console.Write("She'll be coming round the mountain");
Task<int> t = SomeLongJobAsync(); //<--On what thread does this execute?
Console.WriteLine(" when she comes.");
return await t;
}
Run Code Online (Sandbox Code Playgroud)
DoItAsync()执行.SomeLongJobAsync() 开始.WriteLine在DoItAsync()执行.DoItAsync()在 …c# multithreading asynchronous task-parallel-library async-await