如何在进程退出后启用表单按钮?
我有一个使用 C# 开发的 Windows 应用程序。在此应用程序中,我正在创建一个进程。 我想在 Process_Exited() 事件发生时启用和禁用几个按钮。 在 Process_Exited() 方法中,我编写了代码来启用按钮,但在运行时出现错误:
“跨线程操作无效: 控制 'tabPage_button开始提取' 从除以下线程之外的线程访问 它是在其上创建的线程。”
我的代码片段是:
void rinxProcess_Exited(object sender, EventArgs e)
{
tabPage_buttonStartExtraction.Enabled = true;
tabPageExtraction_StopExtractionBtn.Enabled = false;
}
任何人都可以建议如何使这成为可能吗?
I have an windows application developed using C#. In this application, I am creating one process. I want to enable and disable few buttons when Process_Exited() event occures.
In Process_Exited() method, I have written code to enable buttons but at runtime I get error as
"Cross-thread operation not valid:
Control
'tabPage_buttonStartExtraction'
accessed from a thread other than the
thread it was created on."
My code snippet is :
void rinxProcess_Exited(object sender, EventArgs e)
{
tabPage_buttonStartExtraction.Enabled = true;
tabPageExtraction_StopExtractionBtn.Enabled = false;
}
Can anyone suggest how to make this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将启用/禁用行移动到单独的方法中,并使用 Control.Invoke 方法从 rinxProcess_Exited 调用该方法。
Move the enable/disable lines in a separate method and call that method from rinxProcess_Exited using Control.Invoke method.
您正在尝试从不同的线程更改 UI。
尝试这样的事情;
您不应该从另一个线程在 UI 上做太多工作,因为调用非常昂贵。
来源:http://msdn.microsoft.com/en-us/library/ms171728 .aspx
You're attempting to change the UI from a different thread.
Try something like this;
You shouldn't be doing much work on the UI from another thread, as the invocations are quite expensive.
Source: http://msdn.microsoft.com/en-us/library/ms171728.aspx
您必须在 UI 线程上进行 UI 更改。有关更多详细信息,请参阅此问题。
这是应用于您的示例的解决方案:
You must make UI changes on the UI thread. See this question for more details.
Here's the solution applied to your example: