C++/CLI で Windows フォーム アプリケーションを使用するときに、ちょっとしたイライラする問題があります。
問題は、WebClient インスタンスを使用して Web サーバーからファイルをダウンロードする必要があることです。通常、私は DownloadFile を使用し、DownoadFileAsyn は使用しませんが、ダウンロード ファイルの進行状況を示すプログレス バーを表示したい場合は、DownloadFileAsyn を使用する必要があります。では、ダウンロードのプロセスが完了するまで待つにはどうすればよいですか?
コードは次のとおりです。
ref class Example{
private:
static System::Threading::ManualResetEvent^ mre = gcnew System::Threading::ManualResetEvent(false);
public:
void Download();
void DownloadFileCompleted(Object^ sender, System::ComponentModel::AsyncCompletedEventArgs^ e);
};
void Example::Download(){
WebClient^ request = gcnew WebClient;
request->Credentials = gcnew NetworkCredential("anonymous", "anonymous");
request->DownloadFileCompleted += gcnew System::ComponentModel::AsyncCompletedEventHandler(this,&FileCrypt::DownloadFileCompleted);
request->DownloadFileAsync(gcnew Uri("ftp://ftp...."+remote_path),remote_file,mre);
mre->WaitOne();
/*
BLOCK OF INSTRUCTIONS THAT I WANT TO RUN AFTER THE FILE DOWNLOAD IS COMPLETED
*/
}
void Example::DownloadFileCompleted(Object^ sender, System::ComponentModel::AsyncCompletedEventArgs^ e){
MessageBox::Show("COMPLETED");
mre->Set();
}
したがって、ダウンロードが完了すると、プログラムは実行を停止し、mre->WaitOne() 命令の後に、上記の命令ブロックを実行しません。DownloadFileCompleted() は実行されず、実際にはメッセージボックスも表示されます。
何か案は?私はこの問題を探しましたが、多くの人がこの問題を抱えていましたが、c# のみでした。そして、ソリューションをC#からC ++に「翻訳」しました。しかし、うまくいきません...