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

+2 votes
asked Sep 18, 2012 by fraxis

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.

2 Answers

+1 vote
answered Sep 18, 2012 by joel-lucsy

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.

–1 vote
answered Nov 29, 2018 by kpcrash

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.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter