i'm having a little but frustrating problem in my C++/CLI with windows forms application.
So the problem is that i have to download a file from a webserver by using a WebClient istance. Normally i use DownloadFile and not DownoadFileAsyn, but if i want to show a progress bar showing the progress of download file, i MUST use DownloadFileAsyn. So how can i wait the process of downloading until it's finished ?
The code is:
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();
}
So when the download is completed, the program stop running and it doesn't run the block of instructions above written, after the mre->WaitOne() instruction. The DownloadFileCompleted() is not executed, in fact even the messagebox is shown.
Any ideas? I've serarched for this problem, and many people had it, but in c# only. And i've just "translated" the solution from c# to c++. But it doesn't work...