如何在进程退出后启用表单按钮?

发布于 2024-10-10 13:10:52 字数 497 浏览 2 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如歌彻婉言 2024-10-17 13:10:52

将启用/禁用行移动到单独的方法中,并使用 Control.Invoke 方法从 rinxProcess_Exited 调用该方法。

Move the enable/disable lines in a separate method and call that method from rinxProcess_Exited using Control.Invoke method.

泪之魂 2024-10-17 13:10:52

您正在尝试从不同的线程更改 UI。
尝试这样的事情;

    private void SetText(string text)
    {
        // InvokeRequired required compares the thread ID of the
        // calling thread to the thread ID of the creating thread.
        // If these threads are different, it returns true.
        if (this.textBox1.InvokeRequired)
        {   
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            this.textBox1.Text = text;
        }
    }

您不应该从另一个线程在 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;

    private void SetText(string text)
    {
        // InvokeRequired required compares the thread ID of the
        // calling thread to the thread ID of the creating thread.
        // If these threads are different, it returns true.
        if (this.textBox1.InvokeRequired)
        {   
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            this.textBox1.Text = text;
        }
    }

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

匿名。 2024-10-17 13:10:52

您必须在 UI 线程上进行 UI 更改。有关更多详细信息,请参阅此问题

这是应用于您的示例的解决方案:

void rinxProcess_Exited(object sender, EventArgs e)
{
    if (this.InvokeRequired)
    {
        this.Invoke((Action)(() => ProcessExited()));
        return;
    }

    ProcessExited();
}

private void ProcessExited()
{
    tabPage_buttonStartExtraction.Enabled = true;
    tabPageExtraction_StopExtractionBtn.Enabled = false;
}

You must make UI changes on the UI thread. See this question for more details.

Here's the solution applied to your example:

void rinxProcess_Exited(object sender, EventArgs e)
{
    if (this.InvokeRequired)
    {
        this.Invoke((Action)(() => ProcessExited()));
        return;
    }

    ProcessExited();
}

private void ProcessExited()
{
    tabPage_buttonStartExtraction.Enabled = true;
    tabPageExtraction_StopExtractionBtn.Enabled = false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文