简体   繁体   中英

Form.Invoke() does not run when form is hidden,

If I take my C# form (WinForms) and make it not visible:

MyForm.Visible = false;

and then in a separate thread that I create; the following code executes:

        this.Invoke(new MethodInvoker(delegate()
                                          {
                                              ProgressBar2.Visible = false;
                                          }));

My application will just freeze and not continue. If MyForm is visible, the above code in the thread runs fine. If it is not visible, the code stops running as soon as it reads the "this.Invoke(new MethodInvoker(delegate()" line. It does not error out, it just sits there (I set a breakpoint to watch it and it just sits there after the "this.Invoke(new MethodInvoker(delegate()").

Any idea as to why this will not execute when my form is hidden? And how I can get around this problem? I am using "this.Invoke(new MethodInvoker(delegate()" because I need to be able to set a Progress Bar control on the MainForm as visible (and I must do this on another thread than the main UI thread) and I want to do that regardless if the form is visible or not.

When the form is hidden, I believe the message pump is no longer running. I do know that the Invoke mechanism requires the message pump to be running.

You might be able to work around the issue by setting the opacity to 0 instead of visibility, tho this has its own issues.

Wrap your form in an element and set its style="display:none;". Setting visible to false causes the object not to render in the client code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM