BackgroundWorker完成时隐藏表单窗口 [英] BackgroundWorker hide form window upon completion

查看:120
本文介绍了BackgroundWorker完成时隐藏表单窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BackgroundWorker进程完成后,我在隐藏表单方面遇到了麻烦.

I'm having some trouble with hiding a form when a BackgroundWorker process is completed.

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表单.

Basically, after the 'submit' button is pressed, the application begins to upload the files to the webserver via a php script. Once the upload is complete, the RunWorkerCompleted method is triggered, opening the formSubmitted form. The issue I'm having is that the processing form does not close once the backgroundworker is complete and the formSubmitted opens directly on top of the processing form - as opposed to what I want, having the processing form close and then open the formSubmitted form.

推荐答案

实际上,您永远不会关闭processing表单:

Well actually you are never closing processing form:

尝试以下操作:

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
}

这篇关于BackgroundWorker完成时隐藏表单窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆