考虑以下代码,目标是设置System.Threading.Thread.CurrentPrincipal 并在另一个窗口中检索它....带有注释的简单代码,以澄清我想要实现的目标。
Task myTask = Task.Factory.StartNew(new Action(() =>
{
// do in another task so that the UI does not freeze
var user = modelLogin.Login(username, password);
// run on the current dispatcher thread to show results of login
currentDispatcher.BeginInvoke(new Action(() =>
{
if (user == null)
{
errorText = "Authentication failed";
}
else
{
// set the principal
// how ever setting the value here, does not really set it on the main thread
// what i mean by this is that when the login window close i want to get
// the principal i set here in another window
// but it does not seem to work that way
// how to propely set this value in this current situation?
System.Threading.Thread.CurrentPrincipal = authentication;
}
ToggleIsBusy();
CommandManager.InvalidateRequerySuggested();
}));
}));
任何想法我怎么做?
答案 0 :(得分:0)
不是设置Thread的CurrentPrincipal,而是 通过在AppDomain上设置它来为所有线程设置它,它将为所有线程设置它,包括主线程 :
AppDomain.CurrentDomain.SetThreadPrincipal(authentication);