C#跨线程调用问题

发布于 2024-11-03 12:24:20 字数 849 浏览 1 评论 0原文

我正在用 C# 编写一个表单应用程序,我需要能够从任何线程更改富文本框的内容,我尝试使用 delegateInvokeRequired,但我所做的委托仍然给我一个跨线程调用错误,并且 InvokeRequired 使表单崩溃,但没有给出错误。 我需要能够从任何线程执行的函数:

    public static void updateSub(int what)
    {
        subDisplay.subBox.Text = tb[what];
    }

我尝试使用的委托:

    public delegate void UpdateDelegateVoid(int what);
    static public UpdateDelegateVoid uSub = new UpdateDelegateVoid(updateSub);
    uSub(0);

我的 InvokeRequired 代码:

    public static void updateSub(int what)
    {
        if (subDisplay.subBox.InvokeRequired)
        {
            subDisplay.subBox.Invoke(new MethodInvoker(finish));
        }
        else
        {
            subDisplay.subBox.Text = tb[what];
        }
    }

我不太确定为什么上面的代码不起作用。谢谢!

I'm writing a form app in c# and I need to be able to change the contents of a Rich Text Box from any thread, I tried using a delegate and InvokeRequired, but the delegate I made still gives me a cross-thread call error, and InvokeRequired crashes the form, without giving an error.
Function I need to be able to execute from any thread:

    public static void updateSub(int what)
    {
        subDisplay.subBox.Text = tb[what];
    }

The delegate I tried to use:

    public delegate void UpdateDelegateVoid(int what);
    static public UpdateDelegateVoid uSub = new UpdateDelegateVoid(updateSub);
    uSub(0);

My InvokeRequired code:

    public static void updateSub(int what)
    {
        if (subDisplay.subBox.InvokeRequired)
        {
            subDisplay.subBox.Invoke(new MethodInvoker(finish));
        }
        else
        {
            subDisplay.subBox.Text = tb[what];
        }
    }

I'm not really sure why the code above isn't working. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

清眉祭 2024-11-10 12:24:20

严格来说,当您检查 InvokeRequired 并发现其为 true 时,您应该将调用编组到同一方法。我不确定它是否能解决您的具体问题(我需要查看更多异常详细信息和代码),但这就是我的意思:

public static void updateSub(int what)
{
    if (subDisplay.subBox.InvokeRequired)
    {
        subDisplay.subBox.Invoke(new Action<int>(updateSub), what);
    }
    else
    {
        subDisplay.subBox.Text = tb[what];
    }
}

如果您遇到“奇怪的行为”,请检查表单是否实际上是在主应用程序线程。在 WinForms 中,这不是强制的(就像在 WPF 中一样),因此创建表单的线程实际上可能不是应用程序的根线程。

Strictly speaking, when you check InvokeRequired and find it's true, you should marshall the call to the same method. I'm not sure it fixes your specific problem (I'd need to see more exception details and code) but this is what I mean:

public static void updateSub(int what)
{
    if (subDisplay.subBox.InvokeRequired)
    {
        subDisplay.subBox.Invoke(new Action<int>(updateSub), what);
    }
    else
    {
        subDisplay.subBox.Text = tb[what];
    }
}

If you're getting "weird behaviour", then check that the form is actually created on the main application thread. In WinForms this isn't forced (as it is in WPF) so it's just possible that the thread that the form was created on isn't actually the root thread of the app.

虐人心 2024-11-10 12:24:20

我主要用这个,而且效果很好。出于完全相同的目的,这就是您的意图。

public void UpdateSub(string message)
{
    subDisplay.subBox.Invoke((Action)delegate {
        subDisplay.subBox.Text = message;
    });
}

希望它对您或其他人有所帮助!

I mostly use this, and it works perfectly. For the exact same purpose are what you are intending.

public void UpdateSub(string message)
{
    subDisplay.subBox.Invoke((Action)delegate {
        subDisplay.subBox.Text = message;
    });
}

Hope it help's your or someone else with it!

岁月蹉跎了容颜 2024-11-10 12:24:20

试试这个 - 如果需要调用,您可以调用相同的方法。

public void UpdateSub(string message)
{
    if (!subDisplay.subBox.InvokeRequired)
    {
        subDisplay.subBox.Text = message;
    }
    else
    {
        var d = new UpdateFormText(UpdateSub);
        Invoke(d, new object[] { message });
    }
}

其中 UpdateFormText 是委托

public delegate void UpdateFormText(string message);

Try this - where you call the same method if an invoke is required.

public void UpdateSub(string message)
{
    if (!subDisplay.subBox.InvokeRequired)
    {
        subDisplay.subBox.Text = message;
    }
    else
    {
        var d = new UpdateFormText(UpdateSub);
        Invoke(d, new object[] { message });
    }
}

Where UpdateFormText is the delegate

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