从另一个线程关闭表单

Mar*_*rek 11 c# multithreading exe winforms

我有这个运行的代码 .exe

string openEXE = @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
                 Process b = Process.Start(openEXE);
                 b.EnableRaisingEvents = true;
                 b.Exited += (netpokl_Closed);
Run Code Online (Sandbox Code Playgroud)

当它关闭时,它调用方法netpokl_Closed.问题是当我insert into netpokl_Closed command- this.Close()这个异常上升时:Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on

我该如何解决?谢谢你的时间和答案.

Ehs*_*san 36

您正在获取异常,因为您试图从除了创建它之外的线程关闭表单.这是不允许的.

像这样做

this.Invoke((MethodInvoker) delegate
        {
            // close the form on the forms thread
            this.Close();
        });
Run Code Online (Sandbox Code Playgroud)