开发者

When using form.ShowDialog() a thread in mainform doesn't work

I have a form I'm showing using form.showDialog() and in the main form I have a thread that is sending a message every x seconds. I notice that while I'm in the window that I just opened the new form that I opened using ShowDialog the 开发者_如何学编程thread doesn't run. How can I make it continue running even when a ShowDialog is used?

Code:

codeshowAllScriptsWindow window = new showAllScriptsWindow(this); 
window.Show();

and in the mainform I have a thread that keeps sending a message but is stopped because of this showdialog. Note that when I use show() it doesn't happen.


ShowDialog() is a blocking call. The thread actually runs, it is busy pumping the message loop for the dialog. This is no different from what happens on the main thread of your program.

Doing this is very unwise, the dialog has no Z-order relationship with the rest of the windows in your app. One classic mishap is that it can disappear behind another window but no good way for the user to find it back. Use Control.BeginInvoke to create the dialog on the UI thread instead. This also ensures that your thread keeps 'running'.


The message pump is "stolen" by your modal dialog you are displaying. You have 2 options:

  1. Don't use modal dialogs (use formShow())
  2. Use a new thread to do the work and use the main thread for UI.


Although it seems unrelated at first glance, you might take a look at this question. The problem you're having is that the form.showDialog() call creates a modal dialog, halting the progression of code on that thread. If you spin another thread and fire that call there, your first thread will continue running as the dialog displays.


Use a System.Threading.Timer not a System.Windows.Form.Timer.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜