I am new to using event handlers and background workers, so it may be something completely obvious to me here. However, I tried to fix this for two days, so I thought that I could also see what someone had to say.
I have a desktop called SqlExpressDownloader. It starts at the beginning of my program, the rest of the work is done, and then it must wait for the operations in the SqlExpressDownloader_DoWork() method to SqlExpressDownloader_DoWork() before continuing. The only problem is that for some reason, when I do while(SqlExpressDownloader.IsBusy) , it always reacts as busy and therefore will always wait.
The code for the event handler is here:
private void SqlExpressDownloader_DoWork(object sender, DoWorkEventArgs e) { string sSource = string.Format("{0}\\{1}", Paths.Settings_Common, "sqlexpr_x64_enu.exe"); Debug.WriteLine(sSource); Debug.WriteLine("http://www.elexioamp.com/Install/redistributables/sql2008r2express/sqlexpr_x64_enu.exe"); if (!System.IO.File.Exists(sSource)) { WebClient oWebClient = new WebClient(); oWebClient.DownloadProgressChanged += DownloadProgressChanged; oWebClient.DownloadDataCompleted += DownloadComplete; oWebClient.DownloadFileAsync(new System.Uri("http://www.elexioamp.com/Install/redistributables/sql2008r2express/sqlexpr_x64_enu.exe"), sSource); while (oWebClient.IsBusy) { Thread.Sleep(100); } e.Result = ""; DownloadFinished = true; } }
I looked at the code and watched it complete this method. I even added return after DownloadFinished = true , but it still reacts as busy. I want to know how to make a background worker react like not busy.
EDIT All events are added to the constructor, as shown below:
SqlExpressDownloader = new BackgroundWorker(); SqlExpressDownloader.DoWork += new DoWorkEventHandler(this.SqlExpressDownloader_DoWork); SqlExpressDownloader.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.SqlExpressDownloader_RunWorkerCompleted);
RunWorkerCompleteEventHandler as follows:
private void SqlExpressDownloader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { Debug.WriteLine("The actions are complete."); } else { Debug.WriteLine("Error in completed work."); } }
But, when I debugged this last one, it didn’t actually cause it.