将WPF调度程序转换为Winforms BGworker? [英] Convert a WPF dispatcher to Winforms BGworker?

查看:76
本文介绍了将WPF调度程序转换为Winforms BGworker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近获得了服务器控制台包装的一些源代码.该程序最初在WPF中使用,部分代码为:

i recently acquired some source code for a console wrapper for a server. The program was originaly in WPF and part of the code was:

    private void ServerProc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        Dispatcher.Invoke(new Action(() =>
        {
            ConsoleTextBlock.Text += e.Data + "\r\n";
            ConsoleScroll.ScrollToEnd();
        }));
    }
    private void ServerProc_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        Dispatcher.Invoke(new Action(() =>
        {
            ConsoleTextBlock.Text += e.Data + "\r\n";
            ConsoleScroll.ScrollToEnd();
            ParseServerInput(e.Data);
        }));
    }

在两个空白处也都具有此注释:

Its also had this annotation in both voids:

//您必须通过Dispatcher进行此操作,因为此方法是 由其他线程调用

// You have to do this through the Dispatcher because this method is called by a different Thread

但是在WinForms中没有这样的东西-有没有办法将其更改为Background Worker或其他东西(我几乎没有做过任何多线程处理)?

However in WinForms there is no such thing - is there a way to change this to a Background worker or something (Ive barely done any multi-threading)?

如果有人可以帮助,那就太好了!

If anyone could help, that be great!

谢谢.

推荐答案

两个方法都是事件处理程序.它们很有可能来自某种侦听代码,我希望它们是从非UI线程(例如,通常在进行侦听的线程池线程)中调用的.您可以通过放置一个断点并查看调试器中的线程窗口来进行检查.
因此,您将需要使用winforms方法从非UI线程更新UI.
如果您进行搜索,那么应该会发现很多有关如何执行此操作的信息.例如
从其他线程更新UI
如何从C#中的另一个线程更新GUI?

Both methods are event handlers. Chances are they are from some kind of listening code and I would expect that they are called from a non UI thread (eg normally a threadpool thread that is doing the listening). You can check that by putting a break point and looking at the threads window in the debugger.
So you will need to apply the winforms way of updating the UI from a non UI thread.
If you search SO you should find quite a lot on how to do that. E.g
Updating UI from a different thread
How to update GUI from another thread in C#?

这篇关于将WPF调度程序转换为Winforms BGworker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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