Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>degorolls is right in suggesting the TPL, your code would look like below (except without the comments)(Also, exceptions MUST be handled in the TPL, so that might make it not worth it, but I dont think it should). The first methods would remain the same, and yes in event-based async programming thread-safety is taken care of (ie: you always return to the same thread you called out from) I also noticed that the text output is all doing = instead of +=, but that is probably more of a problem of typing into overflow So, test1 and test2 will print out at the same time, however everything being spit out from the TPL code should print as it comes in. UI code should not be doing anything that requires too much time, though...only updating the UI. So, do think of this as a point to refactor? Let me know if this helps or if I missed what you were looking for.</p> <pre><code>client.GetDataAsync(); client.GetDataCompleted += new EventHandler&lt;GetDataCompletedEventArgs&gt;(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(() =&gt; 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(()=&gt; { Dispatcher.BeginInvoke(()=&gt;textBlock1.Text += newCopyOfDataToBeWritten) } ); // ... ///////I assume this is the end of the loop? Debug.WriteLine("done"); } </code></pre> <p>.... the below dummied-down code based on what you posted seems to work for me</p> <pre><code> var outsideThread = new Thread(()=&gt; { for(int i = 0; i &lt; 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(()=&gt;{textBlock1.Text += "outer" + i;}); int newI = i; var thread = new Thread(() =&gt; { System.Threading.Thread.Sleep(1000 * newI); Dispatcher.BeginInvoke(() =&gt; { //This will display as it comes in textBlock1.Text += "inner" + newI; }); }); thread.Start(); } }); outsideThread.Start(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload