control.BeginInvoke()无法调用委托

我注意到control.BeginInvoke(委托)有时无法调用委托。 我知道BeginInvoke只是创建一个PostMessage,该消息稍后由应用程序处理(默认情况下,消息后限制为10,000)。 鉴于我们的应用程序不是很复杂,还有其他原因导致它无法执行委托吗? 我的代码如下所示。

class MyClass : Form{ private bool executing = false; private delegate void DelegateBar(string info, int total, bool status, object obj); private void Bar(string info, int total, bool status, object obj){ log("Enterning Bar"); // Update something on UI executing = false; log("Exiting Bar"); } public void foo(){ log("Entering Foo"); executing = true; try{ // do something over the network }catch(Exception e){ // probably network down. Lets not worry about it } DelegateBar barPtr = new DelegateBar(Bar); // Update UI .. call on form : form is a control this.BeginInvoke(barPtr, new object[] {"someInfo", 3, false, null}); log("Exiting Fool"); } public void callMeEveryFiveSeconds(){ if(!executing) foo(); } private delegate void DelegateCallMe(); // execute every 5 seconds. private void timer1_Tick(object sender, EventArgs e) { Delegate del = new DelegateCallMe(callMeEveryFiveSeconds); // appoligies if syntax is not right, it to convey the idea that callMeEveryFiveSeconds is called on the main thread (asynchronously) this.beginInvoke(del, new object[]{}); } } 

我发布的代码看起来不错。 如果这与您使用的代码不符,那么我建议您查找以下内容之一:

1)如果您的消耗任务需要更长的时间,那么FiveSeconds方法会给出每次都不被调用的外观

2)如果消费任务和更新UI的组合导致每次都不调用方法的外观

3)如果消费任务中未显示的任何代码可能改变执行的值(或退出该方法,因为它可以设置为false)