control.BeginInvoke() 无法调用委托

发布于 2024-11-14 11:32:28 字数 1489 浏览 4 评论 0原文

我注意到 control.BeginInvoke(delegate) 有时无法调用委托。据我所知,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[]{});
    }
}

I noticed that control.BeginInvoke(delegate) some times fail to call the delegate. I understand that BeginInvoke just creates a PostMessage and that message is handled by the application later (post message limit by default is 10,000). Given that our application is not very complex, Is there any other reason why it can fail to execute the delegate? My code is something like below.

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[]{});
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆伤 2024-11-21 11:32:28

发布的代码对我来说看起来很好。如果这与您使用的代码不匹配,那么我建议寻找以下其中一项:

1)如果您的消费任务花费更长的时间,则 FiveSeconds 方法会给出每次都不会被调用的外观

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

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

The code looks fine to me as posted. If this does not match your code in use then I would suggest looking for one of the following:

1) If your consuming task takes longer then the FiveSeconds method to give the appearance of not being called every time

2) If the combination of the consuming task and Updating the UI results in the appearance of the method not being called every time

3) If any of the code not shown in the consuming task could alter the value of executing (or exit the method because it can be set to false)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文