I am currently working on a project that needs to use a thread to perform a background process. The question I have is if an event is raised from a business object that creates a new thread, does it need any special handling to process on the UI thread correctly? For example, I have an application that performs a background task and then displays a small popup window that disappears after a second or two. The logic flows this way: 1) New business object created 2) Business object creates a new thread and then raises a completed event 3) The UI captures the completed event and displays the popup. I'm currently doing this and getting a little black box. Using the debug.writeline I'm writing all events handled. The only one being handled is the form.load event. All other events are ignored. However, if I create the popup from a button on the main form I have no trouble. It works correctly. Any help would be appreciated. Thanks! -- Chris Quick
To call a method in a Control on the main thread (message loop thread) from another thread you must call Invoke which will marshall the data onto the main thread if required and carry out execution on the main thread correctly. Doing it from another thread will result in a PostMessage rather than a SendMessage - google this for many deep explanations. But for a quick fix create a 'ShowPopup' method on your form and call something like myForm.Invoke( new MyHandler( myForm.ShowPoup ), null ) James "Chris Q." wrote: > I am currently working on a project that needs to use a thread to perform a > background process. The question I have is if an event is raised from a > business object that creates a new thread, does it need any special handling > to process on the UI thread correctly? > > For example, I have an application that performs a background task and then > displays a small popup window that disappears after a second or two. The > logic flows this way: > > 1) New business object created > 2) Business object creates a new thread and then raises a completed event > 3) The UI captures the completed event and displays the popup. > > I'm currently doing this and getting a little black box. Using the > debug.writeline I'm writing all events handled. The only one being handled is > the form.load event. All other events are ignored. However, if I create the > popup from a button on the main form I have no trouble. It works correctly. > Any help would be appreciated. > > Thanks! > -- > Chris Quick