結果を待つ別のメソッドでラップしたい非同期メソッドを持つサードパーティの DLL があります。
機能を非表示にするクラスを書き始めましたがDoc.Completed、後で DLL によって呼び出されるのthis.version.DownloadFile(this)を待つ方法がわかりませんDoc.Download。
DLL は を呼び出しInitTransfer、その後OnProgressNotify何度か呼び出しますCompleted。OnErrorどの段階でも呼び出すことができますが、Completed常に最後に呼び出されます。InitTransfer、OnProgressNotifyまたはは気にしませんOnError。
同期メソッドでの非同期呼び出しと非同期呼び出しを同期に変えるを読み ましたが、このケースへの回答を適用する方法がわかりません。
私はC#4を使用しています。
public class Doc : SomeInterfaceFromTheDll
{
private readonly IVersion version; // An interface from the DLL.
private bool downloadSuccessful;
public Doc(IVersion version)
{
this.version = version;
}
public bool Download()
{
this.version.DownloadFile(this);
return ??? // I want to return this.downloadSuccessful after Completed() runs.
}
public void Completed(short reason)
{
Trace.WriteLine(string.Format("Notify.Completed({0})", reason));
this.downloadSuccessful = reason == 0 ? true : false;
}
public void InitTransfer(int totalSize)
{
Trace.WriteLine(string.Format("Notify.InitTransfer({0})", totalSize));
}
public void OnError(string errorText)
{
Trace.WriteLine(string.Format("Notify.OnError({0})", errorText));
}
public void OnProgressNotify(int bytesRead)
{
Trace.WriteLine(string.Format("Notify.OnProgressNotify({0})", bytesRead));
}
}