开发者

background worker window controls not rendered

开发者 https://www.devze.com 2023-01-01 13:58 出处:网络
Is there any way t开发者_StackOverflow中文版o load window inside the background worker thread without using showdialog()? the background worker only terminate only after getting some input from the wi

Is there any way t开发者_StackOverflow中文版o 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

}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消