从另一个线程关闭表单 [英] Closing form from another thread

查看:94
本文介绍了从另一个线程关闭表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有运行.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);

当它关闭时,调用方法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

And when it closes it calls method netpokl_Closed. The issue is when I insert into netpokl_Closed command - this.Close() this exception rises: Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on

我该如何解决?谢谢您的时间和答复.

How can I fix it ? Thank you for your time and answers.

推荐答案

之所以会出现异常,是因为您试图从线程(而不是在创建线程的位置)上关闭表单.这是不允许的.

You are getting the exception because you are trying to close the form from a thread other than on what it was created on. This is not allowed.

像这样

this.Invoke((MethodInvoker) delegate
        {
            // close the form on the forms thread
            this.Close();
        });

这篇关于从另一个线程关闭表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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