2

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

1 に答える 1

0

投稿されたコードは私には問題ないようです。これが使用中のコードと一致しない場合は、次のいずれかを探すことをお勧めします。

1) 消費するタスクに時間がかかる場合は、FiveSeconds メソッドを使用して、毎回呼び出されていないように見せます。

2) 消費するタスクと UI の更新の組み合わせにより、メソッドが毎回呼び出されていないように見える場合

3) 消費タスクに表示されていないコードのいずれかが実行中の値を変更する可能性がある場合 (または false に設定できるため、メソッドを終了する場合)

于 2011-06-07T20:24:20.817 に答える