ケース1
これが私のセットアップです。
internal class MyClass
{
private ApiObject apiObject;
private bool cond1;
private bool cond2;
internal MyClass()
{
this.apiObject = new ApiObject();
this.apiObject.ApiStateUpdate += new ApiStateUpdateEventHandler(ApiStateHandler);
//wait for both conditions to be true
}
private void ApiStateHandler(string who, int howMuch)
{
if(who.Equals("Something") && howMuch == 1)
this.cond1 = true;
else if(who.Equals("SomethingElse") && howMuch == 1)
this.cond2 = true;
}
}
両方の条件が true になるのを待つにはどうすればよいですか?
私が行った場合:
while(!(this.cond1 && this.cond2))
{
System.Threading.Thread.Sleep(1000);
}
のコードはApiStateHandler()
決して実行されないようです。
私が行った場合:
while(!(this.cond1 && this.cond2))
{
System.Windows.Forms.Application.DoEvents();
}
これは機能しますが、リソースの浪費とハックのようです。
wait
基本的に、スレッドをブロックせずに方法が必要だと思います。これを行う適切な方法は何ですか?
ケース 2
2 番目のケースはやや類似 (および関連) しており、同じ問題を示しています。
internal class MyClass
{
private ApiNotifyClass apiNotifyClass;
private shouldContinue = false;
internal MyClass()
{
//in addition to the code from above
this.apiNotifyClass = new ApiNotifyClass();
this.apiNotifyClass.ApiFound += ApiNofityFoundEventHandler(ApiNotifyHandler);
}
internal void Send(SomethingToSend somethigToSend)
{
Verifyer verifier = this.apiObject.ApiGet(somethingToSend);
this.apiNotifyClass.ApiAttach(verifier);
//wait for the shouldContinue to be true
this.apiObject.ApiSend(verifier);
this.apiNotifyClass.ApiDetach(verifier);
}
private void ApiNotifyHandler()
{
this.shouldContinue = true;
}
}
を呼び出すSend()
と、Verifier
オブジェクトが作成され、メソッドは がApiNotifyHandler
実行される (つまり、ApiFound
イベントが発生する) のを待ってから、 を呼び出す必要がありApiSend()
ます。
したがって、これはCase 1と同じ状況です。shouldContinue が true になるのをどのように待つべきですか?
非常に長い質問で申し訳ありませんが、私はあなたが私を助けるのを助けるためにできるだけ多くの情報を提供することを考えました.
【アップデート】
私は .Net 2.0 を使わざるを得ません。