1

WaitAllの実装のいくつかで、私は次のコードを見ました

IAsyncResult result1 = Method.BeginInvoke(10, MyCallback, null)
IAsyncResult result2 = Method.BeginInvoke(20, MyCallback, null)
WaitHandle[] waitHandles = new WaitHandle[] { result1.AsyncWaitHandle, result2.AsyncWaitHandle};
WaitHandle.WaitAll(waitHandles)

これは正しいと思いますか?waitHandles配列が作成される前に、呼び出しの1つが完了する可能性はどのくらいありますか?

よろしく、ダナンジェイ

4

1 に答える 1

6

私には理にかなっています。

// Begin invoking the first and second method, and give them a callback
IAsyncResult result1 = Method.BeginInvoke(10, MyCallback, null)
IAsyncResult result2 = Method.BeginInvoke(20, MyCallback, null)

// Any time past the point of invokation, MyCallback could be called.
// Get the wait handles of the async results, regardless of whether they have finished or not
WaitHandle[] waitHandles = new WaitHandle[] { result1.AsyncWaitHandle, result2.AsyncWaitHandle};

// Make sure to wait until the methods have finished.
// They could have finished immediately, and MyCallback could have already been called,
// but we will never get past this line unless they have finished.
WaitHandle.WaitAll(waitHandles)
// We may have waited on the function to finish, or they may have been done before we arrived at the previous line. Either way, they are done now.

何がおかしいと思いますか?

「チャンスは何ですか」と尋ねることは悪い兆候です。発生する可能性があります。つまり、WaitAllを実行する前に、メソッドが完了した場合にプログラムが実行する必要があることを考慮する必要があります。

于 2011-04-12T02:47:36.087 に答える