degorolls是正確的暗示TPL,你的代碼看起來像下面的(除了沒有評論)(另外,異常必須在辦理TPL,所以這可能使它不值得,但我不認爲它應該)。 第一種方法將保持不變,並且是在基於事件的異步編程中,線程安全性得到了處理(即:您總是返回到您調出的同一線程) 我還注意到文本輸出全部正在執行=而不是+ =,但這可能更像是輸入溢出問題 因此,test1和test2會同時打印出來,但是從TPL代碼中吐出的所有東西都應該打印出來。 UI代碼不應該做任何需要太多時間的事情,儘管......只更新UI。那麼,請將此視爲重構的一點嗎? 讓我知道這是否有幫助,或者如果我錯過了你要找的東西。
client.GetDataAsync();
client.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(GetDataCompleted);
void GetDataCompleted(object sender, GetDataCompletedEventArgs e)
{
// Loop through the data
// ...
textBlock1.Text= "test1";
//////Dispatcher should not be needed here as this IS on the main UI thread
Dispatcher.BeginInvoke(() => textBlock1.Text= "test2");
//////Everything that happens here should NOT be on the main UI thread, thus the cross-thread access exception
//////You can do Dispatcher.CheckAccess to determine if you need to invoke or not
//////Notice the newCopyOfDataToBeWritten. This is a closure,
//////so using the same referenced object will result in errant data as it loops
//////Also, doing it this way does not guarantee any order that this will be written out
//////This will utilize the parallel fully, but there are ways to force the order
var task = Task.Factory.StartNew(()=>
{
Dispatcher.BeginInvoke(()=>textBlock1.Text += newCopyOfDataToBeWritten)
}
);
// ...
///////I assume this is the end of the loop?
Debug.WriteLine("done");
}
.... 以下空置向下代碼根據你發佈什麼似乎爲我
var outsideThread = new Thread(()=>
{
for(int i = 0; i < 20; i++)
{
//This code will show all at once since it is on the main thread,
//which is still running
//If you want this to display one at a time also, then you need
//to use threads and callbacks like below, also
Dispatcher.BeginInvoke(()=>{textBlock1.Text += "outer" + i;});
int newI = i;
var thread = new Thread(() =>
{
System.Threading.Thread.Sleep(1000 * newI);
Dispatcher.BeginInvoke(() =>
{
//This will display as it comes in
textBlock1.Text += "inner" + newI;
});
});
thread.Start();
}
});
outsideThread.Start();
工作(是嗎?一個更好的方法)的Silverlight您正在使用什麼版本?新的Task.Factory.FromAsync工程是一種享受。您擺脫了事件處理程序,代碼變得更加可讀,並且您可以更好地控制事件執行的地點/時間。 – 2012-02-17 04:44:24