我知道System.Threading.Thread.CurrentPrincipal会在.Net 4.0+中进行大多数异步调用。但是,看起来它在调用AsyncResult回调之前被重置。我知道在这个特殊情况下我需要改变什么来解决它,但我想知道为什么框架在这里做(而不是在调用AsyncResult回调之后)。
作为第二个问题 - AsyncResult回调是否总是在" work"的同一个线程上运行。被执行了,或者这不保证?
System.Threading.Thread.CurrentPricipal = CustomPrincipal;
//ManagedThreadID is 101 (for example)
Promise.When(() => {
//System.Threading.Thread.CurrentPrincipal still Custom Principal
//As expected
//ManagedThreadID is 254 (for example - a different thread, as expected)
});
public static IPromise<T> When<T>(Func<T> action)
{
//System.Threading.Thread.CurrentPrincipal still CustomPrincipal
//As expected
Promise<T> p = new Promise<T>();
action.BeginInvoke((result) =>
{
//System.Threading.Thread.CurrentPrincipal NOT CustomPrincipal (it's now a GenericPrincipal object) - reset by framework?
//ManagedThreadId is still 254 (as expected)
try
{
p.Complete(action.EndInvoke(result));
}
catch (Exception ex)
{
p.Complete(ex);
}
}, null);
return p;
}