简体   繁体   English

隐藏表单时,Form.Invoke()无法运行,

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

If I take my C# form (WinForms) and make it not visible: 如果我采用C#表单(WinForms)并使其不可见:

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. 如果MyForm是可见的,则线程中的上述代码运行良好。 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()"). 如果看不到,则代码在读取“ this.Invoke(new MethodInvoker(delegate()”)行后立即停止运行。不会出错,它只是坐在那里(我设置了一个断点来观看它,只是坐在“ 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. 我正在使用“ this.Invoke(new MethodInvoker(delegate())”,因为我需要能够在MainForm上将进度条控件设置为可见(并且我必须在除主UI线程之外的其他线程上执行此操作),我想要无论表单是否可见,都可以这样做。

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. 我确实知道Invoke机制要求消息泵正在运行。

You might be able to work around the issue by setting the opacity to 0 instead of visibility, tho this has its own issues. 您可能可以通过将不透明度设置为0而不是可见性来解决此问题,因为这有其自身的问题。

Wrap your form in an element and set its style="display:none;". 将表单包装在元素中,然后设置其样式=“ display:none;”。 Setting visible to false causes the object not to render in the client code. 将visible设置为false会导致该对象无法在客户端代码中呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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