control.BeginInvoke()无法调用该委托 [英] control.BeginInvoke() fails to call the delegate

查看:209
本文介绍了control.BeginInvoke()无法调用该委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,control.BeginInvoke(代表)有时无法调用该委托。据我了解,仅仅的BeginInvoke创建一个PostMessage的和该消息是由应用程序处理后(POST信息由默认的限额为10,000)。考虑到我们的应用程序是不是很复杂,有没有其他的原因,它可能无法执行委托?我的代码是类似下面

  MyClass类:表格{

私人布尔执行= FALSE;
私人委托无效DelegateBar(字符串信息,INT总,BOOL状态,obj对象);
私人无效栏(字符串信息,INT总,BOOL状态,obj对象){
日志(Enterning酒吧);
//更新UI上
执行的东西= FALSE;
日志(退出吧);
}
公共无效美孚(){
日志(输入富);
执行= TRUE;
尝试{
//做在网络上
的东西}赶上(例外五){
//可能网络中断。让我们不要担心
}
DelegateBar barPtr =新DelegateBar(酒吧);
//更新UI ..呼吁的形式:形式是控制
this.BeginInvoke(barPtr,新的对象[] {someInfo,3,假,空});
日志(退出傻瓜);
}

公共无效callMeEveryFiveSeconds(){
如果foo的()(执行!);
}

私人委托无效DelegateCallMe();

//执行每5秒。
私人无效timer1_Tick(对象发件人,EventArgs五)
{
代表德尔=新DelegateCallMe(callMeEveryFiveSeconds);
// appoligies如果语法不正确的,它传达的想法,callMeEveryFiveSeconds被称为主线程(异步)
this.beginInvoke(德尔,新的对象[] {});
}
}


解决方案

的代码看起来很好,我为公布。如果不使用你的代码匹配,那么我会建议寻找下列之一:



1)如果你的耗时的任务需要更长的时间则FiveSeconds方法给的不外观被称为每次



2)如果耗时的任务和更新的方法的外观的用户界面的结果的组合没有被调用每次

3)任何在耗时任务中未示出的代码可能会改变执行的值(或退出的方法,因为它可以被设置为假)


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

解决方案

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)

这篇关于control.BeginInvoke()无法调用该委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆