Saturday, June 2, 2018

c# - 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.


Answer




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.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...