Trying to understand Task.Wait(). Not doing what I thought it would do.

  • Thread starter Thread starter m00n
  • Start date Start date
M

m00n

Guest
I've tried to read up and understand .NET Task.Wait(). As I understand it, Wait tells the calling thread to stop execution until the task is done executing...

RunningTwoWithWait() gets called by the UI thread (button click) in a Windows Form app. Once t1.Wait(); is called, my GUI freezes up. As you can see, UpdateText() is not doing very much. Just writing text to a text box 500 times, which executes pretty quick.

All I wanted to test out was trying to force T2 to wait to execute until after T1. I did achieve this affect with calling ContinueWith, but I was hoping achieve it by waiting.

It seems as though I definitely don't fully grasp what Wait is doing. Was hoping someone could explain to me why this is doing what I was hoping for.


private void RunningTwoWithWait()
{
var t1 = Task.Factory.StartNew (UpdateText, "ONE");
t1.Wait( );
var t2 = Task.Factory.StartNew (UpdateText, "TWO");
}


private void UpdateText( object state )
{
for( int i = 0; i < 500; i++ )
{
richTextBox1.Invoke( new Action( ( ) => richTextBox1.Text += "\n" + state + " " + i ) );
}
}


Thanks for any help!!!

Rick



Rick

Continue reading...
 
Back
Top