【问题标题】:BackgroundWorker hide form window upon completionBackgroundWorker 完成后隐藏表单窗口
【发布时间】:2013-08-02 07:37:48
【问题描述】:

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 表单。我遇到的问题是,一旦后台工作人员完成并且formSubmitted 直接在processing 表单顶部打开,processing 表单不会关闭 - 与我想要的相反,拥有processing 表单关闭然后打开formSubmitted 表单。

【问题讨论】:

  • 包含上述所有方法的类是什么(this 引用的方法)?
  • 班级是uploadPanel

标签: c# winforms backgroundworker


【解决方案1】:

其实你永远不会关闭processing 表单:

尝试以下:

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();
}

现在完成隐藏processing 表单:

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
}

【讨论】:

  • 正如 OP 评论的那样,this 始终是uploadPanel(可能是Panel 的类型)。
  • 好的,现在我知道我哪里出错了。您的代码建议可以正常工作。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
相关资源
最近更新 更多