BackgroundWorker hides the form window upon completion

I am having trouble hiding the form when the BackgroundWorker process is complete.

private void submitButton_Click(object sender, EventArgs e) { processing f2 = new processing(); f2.MdiParent = this.ParentForm; f2.StartPosition = FormStartPosition.CenterScreen; f2.Show(); this.Hide(); backgroundWorker1.RunWorkerAsync(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { // loop through and upload our sound bits string[] files = System.IO.Directory.GetFiles(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + "\\wav", "*.wav", System.IO.SearchOption.AllDirectories); foreach (string soundBit in files) { System.Net.WebClient Client = new System.Net.WebClient(); Client.Headers.Add("Content-Type", "audio/mpeg"); byte[] result = Client.UploadFile("http://mywebsite.com/upload.php", "POST", soundBit); } } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { formSubmitted f3 = new formSubmitted(); f3.MdiParent = this.ParentForm; f3.StartPosition = FormStartPosition.CenterScreen; f3.Show(); this.Hide(); } 

Basically, after clicking the "Submit" button, the application starts uploading files to the web server via php script. After the download is complete, the RunWorkerCompleted method is launched, which opens the formSubmitted form. The problem I am facing is that the processing form does not close after the background worker completes, and the formSubmitted opens directly on top of the processing form - unlike what I want, having the processing form then open the formSubmitted form.

0
source share
1 answer

In fact, you never close the processing form:

try the following:

 private processing _processingForm; private void submitButton_Click(object sender, EventArgs e) { _processingForm = new processing(); _processingForm.MdiParent = this.ParentForm; _processingForm.StartPosition = FormStartPosition.CenterScreen; _processingForm.Show(); this.Hide(); //HIDES THE CURRENT FORM CONTAINING SUBMIT BUTTON backgroundWorker1.RunWorkerAsync(); } 

Now, when done, hide the processing form:

 private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { formSubmitted f3 = new formSubmitted(); f3.MdiParent = this.ParentForm; f3.StartPosition = FormStartPosition.CenterScreen; _processingForm.Close();//CLOSE processing FORM f3.Show(); this.Hide();//this REFERS TO THE FORM CONTAINING WORKER OBJECT } 
+1
source

Source: https://habr.com/ru/post/979249/


All Articles