updating winform

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am having issue w/ threading. I have a datagrid and I want to run
FillDG() in different thread and once it finishes, I want to bind the data.

private void button1_Click(object sender, System.EventArgs e)
{
Thread t = new Thread(new ThreadStart(FillDG));
t.Start();

// I don't know what to put here to wait for t to finish
// I tried t.Join(); but this locked up the win form, i.e. I can't move the
form around

myDataGrid.SetDataBinding(myDataSet, "myDataTable");
}

Please advice, thanks!
-P
 
Hi Paul,

First of all i am not sure why you are filling the dataset in a seperate
thread and then waiting in the main thread? Isn't the whole purpose of
starting a new thread being lost here?

When you say t.Join() on the main thread , which is your UI thread and
which has the message pump, you are stopping the thread from running
till the second thread has finished running. This means that your
message pump will not run, no UI messages are processed and hence you
see the unresponsive UI.

I can suggest some workarounds,
One is to use Application.DoEvents() , this method processes any pending
messages in your windows message , so instead of calling join() you can
poll for thread completion in a loop and call Application.DoEvents() if
the thread has not finished yet.

One other solution would be to use a Delegate to call FillDG
asynchronously and supply a callback method that will get called when
the thread has finished executing. You can then bind the data in the
callback method.

Take a look at these articles
http://msdn.microsoft.com/msdnmag/issues/04/01/BasicInstincts/default.aspx
http://msdn.microsoft.com/library/d...conasynchronousdelegatesprogrammingsample.asp
http://msdn.microsoft.com/library/d...ml/cpconasynchronousdesignpatternoverview.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms08162002.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms01232003.asp

Let me know if need further help.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 
Hi Sijin,

Thanks for the reply. The last 3 links you suggested are exactly what I need.

Thanks again!
-P
 
Back
Top