Another thread question
-
My app was never designed to support threading - The GUI has a VB6 front end but I have rewritten it in VB.NET with a C# backend - don't ask :wtf: Some of the operations in the C# back end can take a long time are thus good candidates for running on a worker thread - these operations communicate back to the GUI using events - status information, adding rows to a datagrid, progress bar etc. As I am new to threads in .NET can I still use the events from the function running on the worker thread but just change the event sinks so they use BeginInvoke to update the GUI controls? The class which contains the long running operations has some database code in it - can I call that code from the long running operation on the new thread just as I would if it wasn't running on the thread? I don't believe I have any synchronisation issues as the database is always read only - the only reason I want the long running operation to run on a separate thread is so the GUI remains responsive, I dont actually intend to let the user do anything with the GUI other than maybe cancel the long running operation. TIA
-
My app was never designed to support threading - The GUI has a VB6 front end but I have rewritten it in VB.NET with a C# backend - don't ask :wtf: Some of the operations in the C# back end can take a long time are thus good candidates for running on a worker thread - these operations communicate back to the GUI using events - status information, adding rows to a datagrid, progress bar etc. As I am new to threads in .NET can I still use the events from the function running on the worker thread but just change the event sinks so they use BeginInvoke to update the GUI controls? The class which contains the long running operations has some database code in it - can I call that code from the long running operation on the new thread just as I would if it wasn't running on the thread? I don't believe I have any synchronisation issues as the database is always read only - the only reason I want the long running operation to run on a separate thread is so the GUI remains responsive, I dont actually intend to let the user do anything with the GUI other than maybe cancel the long running operation. TIA
-
Sorry, posted it twice - the first attempt too 20 minutes then went blank - when I looked it wasn't there so I reposted it - naturally the first one decided to appear in the meantime :mad: Rugby League: The Greatest Game Of All.
Multithreading w/ a GUI on MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp[^]
-
Multithreading w/ a GUI on MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp[^]
-
Thanks, I have read those articles and also in his book on Windows Forms programming where the articles are repeated. They don't really address my events question though.
Yes, you can still use evens. The article details exactly how to do it in terms of your GUI. First, subscribe to the events or use a delegate which points to your GUI's method. Either way will work fine. Your thread will call it's delegate or raise its event, and the UI thread will receive the notification. In your gui you'll need code similar to this to handle the event:
private delegate void uiThreadDelegate(object sender, EventArgs e);
protected void MyEventHandler(object sender, EventArgs e)
{
if(this.InvokeRequired)
{
uiThreadDelegate ui = new ThreadDelegate(MyEventHandler);
this.BeginInvoke(ui, new object[]{sender, e});
}else{
... your code
}
}