我有一個WPF應用程序,它使用一些庫代碼進行身份驗證,需要在單線程公寓線程中運行。我的方法是產生一個單獨的線程來獲取認證對象,阻塞直到線程返回並繼續執行。但是,在某些實例中,即使線程方法已返回,我的應用程序仍掛在Thread.Join()上。即使線程中的方法已返回,Thread.Join()爲何仍然掛起?
public static ClaimsAuthenticationResult GetClientContextAndCookieCollection(string siteUrl, out CookieCollection cookieResult)
{
ClaimsAuthenticationResult authResult = new ClaimsAuthenticationResult();
// Authentication module needs to run in single-thread apartment state because it uses
// COM library calls where this is required
Thread authenticationThread = new Thread(new ThreadStart(threadMethod));
authenticationThread.SetApartmentState(ApartmentState.STA);
authenticationThread.Start();
// Block until thread completion
authenticationThread.Join(); // Application hangs here
return authResult;
}
private static void threadMethod() {
// In proper application: set result. But for debugging, return immediately
return;
}
我對於多線程和WPF都是新手,所以我可能會做一些愚蠢的事情。有人看到這裏發生了什麼?爲了記錄,如果我沒有將線程設置爲STA,我不會遇到問題,但這是一項要求。
[編輯:似乎只有當我通過WPF視圖中的驗證綁定調用指定的方法時,特別是在TextBox上纔會出現此錯誤。當我在視圖的構造函數中調用相同的代碼時,代碼按預期運行。這將是一個可行的解決方法,但它會很有趣,知道這裏實際發生了什麼。]
[編輯:這裏的代碼已經簡化了一點調試 - 在生產代碼中,線程方法是在AuthThreadWorker對象,用於將身份驗證過程的結果返回給authResult對象。但是這些細節只要我可以告訴與凍結無關,即使在簡化代碼中凍結也是如此。]
你確定線程的方法已經完成了嗎? –
嘗試附加一個debbuger並在'return'上添加一個斷點檢查你是否真的打了它。如果是這樣,請檢查哪個線程。 – Guillaume
線程方法實際上做了什麼,我沒有看到發佈的代碼有什麼問題? – Jodrell