Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关闭在另一个线程中创建的窗体_C#_Multithreading_Winforms - Fatal编程技术网

C# 关闭在另一个线程中创建的窗体

C# 关闭在另一个线程中创建的窗体,c#,multithreading,winforms,C#,Multithreading,Winforms,我一直在寻找我的问题的答案,但没有成功 我的程序中有一个任务需要几秒钟,我想在任务完成时显示一个新表单。新表单有一个加载栏和一些文本 我需要显示与任务平行的新表单,否则在我关闭新表单之前任务不会启动 这就是我现在的解决方案: private void loadingBar() { frmLoading frm = new frmLoading("Please wait while the database is being backed up", "This might take sev

我一直在寻找我的问题的答案,但没有成功

我的程序中有一个任务需要几秒钟,我想在任务完成时显示一个新表单。新表单有一个加载栏和一些文本

我需要显示与任务平行的新表单,否则在我关闭新表单之前任务不会启动

这就是我现在的解决方案:

private void loadingBar()
{
    frmLoading frm = new frmLoading("Please wait while the database is being backed up", "This might take several days.");
    frm.ShowDialog();  

}

public void Backup()
{
    Thread load = new Thread(new ThreadStart(loadingBar));
    load.Start();

    ///Execute a task.

    load.Abort(); 
}

因此,这可以正常工作,但我的问题是:在加载线程中关闭表单“frm”以使其停止不是更好吗?

您可以在类级别声明表单,然后通过调用将其关闭。

像这样:

    public class Class1
{
    private Form myForm;

    public Class1()
    {
        myForm = new Form();
    }

    public void DoSomeWork()
    {
        // ===================================================
        // Do Some Work...
        // ===================================================

        myForm.Invoke(new MethodInvoker(this.Hide));
    }

    public void Hide()
    {
        myForm.Hide();
    }

    public void Backup()
    {
        myForm.ShowDialog();

        Thread load = new Thread(new ThreadStart(DoSomeWork));
        load.Start();
    }
}

你可以用几种方法来做这件事

1-你可以按照BendEg的建议做,一旦准备好就调用frmClose

有点像

Invoke(new Action(Close)); 

2-或者你可以简单地使用背景工人

证明这一点的最简单方法是向表单中添加BackgroundWorker,并使用提供的事件

public Form1()
{
    InitializeComponent();
    backgroundWorker1.RunWorkerAsync();

    MessageBox.Show(@"Please wait while the database is being backed up", @"This might take several days.");
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    Debug.WriteLine("Running"); //Execute a task
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    Debug.WriteLine("Ended"); //Dispose of any objects you'd like (close yor form etc.)
}

我希望这会有所帮助。

我想这会对你有所帮助(如果我理解正确的话):

我以方法为例演示了两个正在运行的任务


您需要使用正确的using语句
using System.Threading.Tasks

我想这对你有用

void YourMethod()
{
     WaitForm wf = new WaitForm();
     Invoke(new PleaseWaitDelegate(Launch),wf);
     bool val = BoolMethodDoWork();
     Invoke(new PleaseWaitDelegate(Close), wf);
                if(val)
                {
                    MessageBox.Show("Success!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                    return;
                }
                MessageBox.Show("Damn!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
delegate void PleaseWaitDelegate(Form form);
        void Launch(Form form)
        {
            new System.Threading.Thread(()=> form. ShowDialog()).Start();
        }

        void Close(Form form)
        {
            form.Close();
        }

将其视为进度条工作。将“进程”作为要显示的窗体的后台线程运行。然后从“主”线程调用该窗体。当加载窗体作为后台线程运行时,主线程将锁定并显示加载窗体。这样,加载表单本身就可以跟踪流程并在流程结束后关闭自己。@Dave为什么不使用
backgroundWorker
?此外,您可能需要考虑在<代码>使用(<)/>代码>语句中为线程放置该代码,然后将在完成时自行处理。这基本上是错误的。在工作线程上显示UI有几种非常严重的故障模式。您需要以另一种方式执行此操作,在UI线程上显示“加载”窗口,在工作线程上执行备份。。。大约有20%的时间以ThreadAbortException结束。我面临着完全相同的问题。但是我需要显示和隐藏我使用单独线程设计的已经存在的表单(而不是像上面那样使用线程创建)。采取什么方法来做到这一点?
                Parallel.Invoke(() => somemethod(), () =>
                {
                    someothertaskmethod();
                });
void YourMethod()
{
     WaitForm wf = new WaitForm();
     Invoke(new PleaseWaitDelegate(Launch),wf);
     bool val = BoolMethodDoWork();
     Invoke(new PleaseWaitDelegate(Close), wf);
                if(val)
                {
                    MessageBox.Show("Success!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                    return;
                }
                MessageBox.Show("Damn!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
delegate void PleaseWaitDelegate(Form form);
        void Launch(Form form)
        {
            new System.Threading.Thread(()=> form. ShowDialog()).Start();
        }

        void Close(Form form)
        {
            form.Close();
        }