.NET:如何有后台线程信号主线程数据是可用的? [英] .NET: How to have background thread signal main thread data is available?

查看:110
本文介绍了.NET:如何有后台线程信号主线程数据是可用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是正确的技术,有的ThreadA 信号 ThreadB 的一些事件,而无需 ThreadB 坐阻塞,等待一个事件的发生?

What is the proper technique to have ThreadA signal ThreadB of some event, without having ThreadB sit blocked waiting for an event to happen?

我有一个后台线程将被填充共享列表< T&取代。我试图找到一种方法来异步信号主线程有数据可被拾起。

i have a background thread that will be filling a shared List<T>. i'm trying to find a way to asynchronously signal the "main" thread that there is data available to be picked up.


我认为设定的事件与的EventWaitHandle对象,但我不能有我的主线程坐在一个Event.WaitOne()。

i considered setting an event with an EventWaitHandle object, but i can't have my main thread sitting at an Event.WaitOne().


我认为有一个委托的回调,但 一)我不想让主线程做工作,在委托:线程需要回去工作增加更多的东西 - 委托执行,而我不希望它在等待,和 二)委托需要被整理到主线程,但我不运行的UI,我管不着,以.Invoke对委托。

i considered having a delegate callback, but a) i don't want the main thread doing work in the delegate: the thread needs to get back to work adding more stuff - i don't want it waiting while the delegate executes, and b) the delegate needs to be marshalled onto the main thread, but i'm not running a UI, i have no Control to .Invoke the delegate against.


我认为有一个简单的启动零间隔System.Windows.Forms.Timer(与线程访问计时器同步的)的委托回调。这样,线程只需要为它调用被卡住

i considered have a delegate callback that simply starts a zero interval System.Windows.Forms.Timer (with thread access to the timer synchronized). This way the thread only needs to be stuck as it calls

Timer.Enabled = TRUE;

但是,似乎是一个黑客攻击。

but that seems like a hack.

在古时我的对象将创建一个隐藏的窗口,并有主题发表消息的隐藏的窗口HWND。我认为创建一个隐藏的控制,但据我了解,你不能用创建无手柄控制.Invoke。另外,我有没有用户界面:我的目标本来是一个web服务器,服务,或控制台上创建的,我不希望有是一个图形化的控制出现 - 我也不想要编译在System.Windows的依赖。形式。

In the olden days my object would have created a hidden window and had the thread post messages to that hidden windows' HWND. i considered creating a hidden control, but i gather that you cannot .Invoke on a control with no handle created. Plus, i have no UI: my object could have been created on a web-server, service, or console, i don't want there to be a graphical control appearing - nor do i want to compile a dependency on System.Windows.Forms.


我考虑有我的目标暴露一个ISynchronizeInvoke接口,但后来我需要实现.Invoke(),这就是我的问题。

i considered having my object expose an ISynchronizeInvoke interface, but then i would need to implement .Invoke(), and that's my problem.


什么是正确的技术纷纷跟帖的一些事件的信号线程B,而无需线程B坐阻塞等待一个事件的发生?

What is the proper technique to have thread A signal thread B of some event, without having thread B sit blocked waiting for an event to happen?

推荐答案

下面是对System.ComponentModel.BackgroundWorker类code样本。

Here's a code sample for the System.ComponentModel.BackgroundWorker class.

    private static BackgroundWorker worker = new BackgroundWorker();
    static void Main(string[] args)
    {
        worker.DoWork += worker_DoWork;
        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        worker.ProgressChanged += worker_ProgressChanged;
        worker.WorkerReportsProgress = true;

        Console.WriteLine("Starting application.");
        worker.RunWorkerAsync();

        Console.ReadKey();
    }

    static void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        Console.WriteLine("Progress.");
    }

    static void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        Console.WriteLine("Starting doing some work now.");

        for (int i = 0; i < 5; i++)
        {
            Thread.Sleep(1000);
            worker.ReportProgress(i);
        }
    }

    static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Console.WriteLine("Done now.");
    }

这篇关于.NET:如何有后台线程信号主线程数据是可用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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