简体   繁体   English

关闭表单后,“从创建该线程的线程以外的线程访问控件”

[英]“Control accessed from a thread other than the thread it was created on” after closing form

I have a problem and don't know how to solve it. 我有一个问题,不知道如何解决。 I've got a Form2 which I open from my Form1 by a button click. 我有一个Form2,可以通过单击按钮从Form1中打开它。 In this form2 I do some operations on a ListView. 在此form2中,我对ListView进行了一些操作。 While the operations are working in the background I want to show another form (Form3) which gets opened in that time. 当操作在后台运行时,我想显示另一个在该时间打开的窗体(Form3)。 So I did the following: 所以我做了以下事情:

public void method()
 {
  Form3 form3 = Form3.GetInstance();

        if (thrd == null)
        {
            thrd = new Thread(new ThreadStart(showForm));
            thrd.IsBackground = true;
            thrd.Start();
        }

        else
        {
            if (form3.InvokeRequired)
            {
                form3.Invoke((MethodInvoker)delegate()
                {
                    form3.Show();
                }
                );
            }
            else
            {
                form3.Show();
            }
        }

     //some operations getting invoked

        if (form3.InvokeRequired)
            {
                form3.Invoke((MethodInvoker)delegate()
                {
                    form3.Hide();
                }
                );
            }
            else
            {
                form3.Hide();
            }
}


   private void showForm()
    {
        Form3 form3 = Form3.GetInstance();
        Application.Run(Form3.GetInstance());
    }

And the code from the Form3: 以及来自Form3的代码:

    private static Form3 m_instance = null;
    private static object m_instanceLock = new object();

    public static Form3 GetInstance()
    {

        lock (m_instanceLock)
        {
            if (m_instance == null)
            {
                m_instance = new Form3();
            }
        }

        return m_instance;
    }

So when I open the Form2 and do my stuff there everything works fine. 因此,当我打开Form2并执行我的工作时,一切正常。 When I call operations that need longer then the Form3 shows up and closes when operations are done. 当我调用需要较长时间的操作时,Form3将显示并在操作完成后关闭。 But when I close my Form2 and then open it again (without closing Form1) then I get the Exception mentioned above in the headline at the Application.Run method. 但是,当我关闭Form2然后再次打开它(不关闭Form1)时,我在Application.Run方法的标题中得到了上面提到的异常。 Why? 为什么?

Ok this may not be the best way but I solved the problem now. 好的,这可能不是最好的方法,但是我现在解决了这个问题。 I just had to put the code where he creates the Thread, shows the form and hides the form into my Form1. 我只需要将代码放在他创建线程,显示表单并将表单隐藏到我的Form1中的位置即可。 This prevents him crashing when the form gets opened a second time. 这样可以防止他在第二次打开表单时崩溃。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 跨线程操作无效:控制''从窗口形式创建的线程以外的线程访问 - Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on in window form 在不是在其上创建线程的线程上访问控件 - Control accessed on a thread other than the thread it was created on 从不是在其上创建线程的线程访问的控件“ x” - Control 'x' accessed from a thread other than the thread it was created on 从不是在C#中创建的线程的线程访问控件 - Control accessed from a thread other than the thread it was created on in C# 从不是在其上创建的线程的线程访问控件“ webBrowser1” - Control 'webBrowser1' accessed from a thread other than the thread it was created on 从不是在其上创建线程的线程访问控件“ dataGridView1” - Control 'dataGridView1' accessed from a thread other than the thread it was created on 从不是在其上创建线程的线程访问控件“ ZedGraphControl” - Control 'ZedGraphControl' accessed from a thread other than the thread it was created on ParallelFor | 跨线程操作无效:从创建该线程的线程以外的线程访问控件 - ParallelFor | Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on 跨线程操作无效:从不是在其上创建线程的线程访问控件“ lbDatabase” - Cross-thread operation not valid: Control 'lbDatabase' accessed from a thread other than the thread it was created on 跨线程操作无效:控件“ listBox2”是从不是其创建线程的线程访问的 - Cross-thread operation not valid: Control 'listBox2' accessed from a thread other than the thread it was created on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM