views:

128

answers:

2

Is there any way to load window inside the background worker thread without using showdialog()? the background worker only terminate only after getting some input from the window. Here the issue is window shown but the button and other controls are not rendered even we don't have control over any of the window.

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
{
        // acquire form
    Acquire aq = new Acquire(Handle);
        aq.Show();
        do
        {
    // waiting for image

        } while (!aq.isImageReady);      

    // doing Image operation

}
A: 

You can show the dialog before you start the background worker, and declare a volatile var that you can change/check in the background worker and keep it running until it will have your desired value, that will be achieved once the dialog is closed.

Adrian Faciu
As per my project architecture, I can't show the dialog before starting background worker.
senthil
A: 

The problem might be because you're using the background thread to show the window instead of the main UI thread of the process. Winform controls either throw exceptions or behave incorrectly if they are not used on the proper thread. In this case, the problem might be that your main window is running on the main UI thread while this additional dialog window is being created and shown by a different thread.

Try raising an event from the background thread to let the UI know that it requires input from the user. The UI can then handle displaying the dialog and responding to the user's input by passing the data back to the background thread.

In order to prevent the background thread from proceeding any further, create a System.Threading.AutoResetEvent object (a WaitHandle) and call the WaitOne method on that object immediately after raising the event to notify the UI to show the dialog. When the UI responds to the user input by passing the data back to the background thread, that code can call the Set method on the AutoResetEvent object, allowing the background thread to proceed.

Dr. Wily's Apprentice