当 BackgroundWorker 进程完成时,我在隐藏表单时遇到了一些麻烦。
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();
}
基本上,在按下“提交”按钮后,应用程序开始通过 php 脚本将文件上传到网络服务器。上传完成后,会触发 RunWorkerCompleted 方法,打开formSubmitted
表单。我遇到的问题是,一旦后台工作人员完成并且直接在表单顶部打开,processing
表单就不会关闭- 与我想要的相反,关闭表单然后打开表单。formSubmitted
processing
processing
formSubmitted