C# BackgroundWorker完成后隐藏表单窗口

C# BackgroundWorker完成后隐藏表单窗口,c#,winforms,backgroundworker,C#,Winforms,Backgroundworker,当BackgroundWorker进程完成时,我在隐藏表单方面遇到了一些问题 private void submitButton_Click(object sender, EventArgs e) { processing f2 = new processing(); f2.MdiParent = this.ParentForm; f2.StartPosition = FormStartPosition.CenterScreen; f2.Show(); t

当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脚本将文件上传到Web服务器。上传完成后,会触发RunWorkerCompleted方法,打开
formSubmitted
表单。我遇到的问题是,后台工作人员完成后,
处理
表单不会关闭,
表单提交
会直接在
处理
表单上打开,这与我想要的相反,关闭
处理
表单,然后打开
表单提交
表单。

事实上,您从未关闭
处理
表单:

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
}
请尝试以下操作:

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();
}
现在在完成时隐藏
处理
表单:

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
}

包含上述所有方法的类是什么(this引用了这些方法)?该类是
uploadPanel
,正如OP所评论的,
this
始终是
uploadPanel
(可能是
Panel
的类型)。好的,现在我知道哪里出了问题。您的代码建议可以正常工作。谢谢你的帮助。