-1

簡単な説明は、私が制御できないサードパーティの DLL でいくつかの接続イベントを開始しようとしているということです。接続が発生すると、イベントが発生します。

これが発生するまでメインスレッドで待機する必要がありますが、これを設定するたびにメインスレッドがロックされます。

private EventWaitHandle _waitForChannel = new AutoResetEvent(false);

呼び出しコード

new Thread(StartInstrumentMonitoring).Start();

=-------------------

public void StartInstrumentMonitoring(){
if(this.instrumentList.Count == 0){
    new Thread(startConnections).Start()
_waitForChannel.WaitOne();

=---------------------------------

public void startCOnnections(){
 CODE HERE TO DETERMINE INSTRUMENT
 instrument.connectionReceived += instrument_connectionEvent;

instrument_connectionEvent(object sender, ResultEventArgs e){

            if (sender.Name == instrument.Identifier || sender.Identifier == instrument.Identifier)
                _waitForChannel.Set();
}

このコードの問題は、実行時に StartInstrumentMonitoring がサード パーティの DLL に対して必要な呼び出しを行い、イベント ハンドラーをアタッチしてライブラリと対話した後に戻ることです。これは明らかに WaitOne() を呼び出します。これは、私が使用しているメイン スレッドをブロックし、instrument_ConnectionEvent

4

1 に答える 1

0

がまったく呼び出されていない場合、instrument_ConnectionEventサードパーティのライブラリがメイン スレッドでイベントをトリガーしようとしている可能性があり、現在のコードでデッドロックが発生する可能性があります。

代わりにAutoResetEvent、単純な条件付きチェックを使用して、接続が確立されているかどうかを確認できます。Application.DoEvents()ループ内で呼び出して、メッセージ キューを強制的に処理できます。

while(<check condition> == false)
{
    Application.DoEvents();
}
于 2015-10-07T16:39:32.600 に答える