Re:线程通信 [英] Re: thread communication

查看:53
本文介绍了Re:线程通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谢谢Chris,

看起来不错,但我想念双向沟通。在主线程中,

向工作线程提供参数和数据 - 我该怎么做?

问候

Ronny

查看后台工作线程组件。它有你想要内置的


http://msdn.microsoft.com/en-us/library/8xs8549b.aspx


Chris

解决方案

2008年10月13日星期一10:19:08 -0700,Ronny< ro *** @ john.comwrote:


谢谢Chris,

看起来不错,但我想念双向沟通。在

的主线程中向工作线程提供参数和数据 - 我该怎么做?



这实际上取决于确切的数据,线程将用它做什么,以及

你想要如何传递数据。


最简单的方法是在启动时将所有数据传递给线程

。 BackgroundWorker类很容易支持。要将数据传递给

a线程,因为它正在执行意味着你需要提出一个特定的

策略,当线程将检索它时传递什么数据,

以及如何。一种可能的方法是使用工作线程

从其他线程写入时读取的队列(参见

Google中的producer / consumer)。但这不是人们可能采取的唯一方法,也不是普遍最适合的b $ b。

除非你能提供更具体的问题,这将非常难以为你的问题提供任何答案。


Pete


谢谢Pete先生,

在我的场景中,主线程应该不时地传递给工作线程一些字符串

。在C ++编程中,我会使用全局数据作为

最简单的机制(虽然不是那么优雅),但在C#中我禁止它。

你可以提供更多的帮助吗?
问候

Ronny

" Peter Duniho" < Np ********* @nnowslpianmk.com在留言中写道

news:op *************** @ petes-computer.local ...


2008年10月13日星期一10:19:08 -0700,Ronny< ro *** @ john.comwrote:
< blockquote class =post_quotes>
>谢谢Chris,
看起来不错,但我想念双向沟通。在主线程中向工作线程提供参数和数据 - 我该怎么做?



这取决于确切的数据,线程将用它做什么,以及

你想要如何传递数据。


最简单的方法是在启动时将所有数据传递给线程

。 BackgroundWorker类很容易支持。要将数据传递给

a线程,因为它正在执行意味着你需要提出一个特定的

策略,当线程将检索它时传递什么数据,

以及如何。一种可能的方法是使用工作线程

从其他线程写入时读取的队列(参见

Google中的producer / consumer)。但这不是人们可能采取的唯一方法,也不是普遍最适合的b $ b。

除非你能提供更具体的问题,这将非常难以为你的问题提供任何答案。


Pete



你可以很容易地创建一个具有

的类A:队列< string>

B :一个工作线程

C:你用来锁定的对象()


当你想要添加数据时,你可以调用一个公共方法来实现


锁定(SyncRoot)

Queue.Enqueue(数据);

线程需要数据时


string data = null;

string hasData = false;

lock(SyncRoot)

{

hasData = Queue.Count 0;

if(hasData)

data = Queue.Dequeue();

}


//处理数据


您可以制作线程休眠,或者您可以使用线程等待的
的AutoResetEvent,并从您的公共方法调用AutoResetEvent.Set()

排队。


-

Pete

====
http://mrpmorris.blogspot.com
http://www.capableobjects.com


Thanks Chris,
Looks nice but I miss the dual way communication. In the main thread to
deliver paramters and data to the worker thread- how can I do that?
Regards
Ronny
Take a look at the background worker thread component. It has what
you want built into it.

http://msdn.microsoft.com/en-us/library/8xs8549b.aspx

Chris

解决方案

On Mon, 13 Oct 2008 10:19:08 -0700, Ronny <ro***@john.comwrote:

Thanks Chris,
Looks nice but I miss the dual way communication. In the main thread to
deliver paramters and data to the worker thread- how can I do that?

It really depends on the exact data, what the thread will do with it, and
how you want the data to be delivered.

The simplest approach is to pass all the data to the thread when you start
it up. The BackgroundWorker class supports this easily. To pass data to
a thread as it''s executing means you need to come up with a specific
strategy of what data is being passed, when the thread will retrieve it,
and how. One possible approach is to use a queue that the worker thread
reads from while other threads write to (see "producer/consumer" in
Google). But that''s hardly the only approach one might take, nor is it
universally the most appropriate.

Unless you can provide a more specific question, it''s going to be very
difficult to provide any sort of answer to your question.

Pete


Thanks Mr. Pete,
In my scenario the main thread should pass to the worker thread some string
from time to time. In C++ programming I would use global data as the
simplest mechanism (though not so elegant)but in C# I guise its forbidden.
Can you farther help?
Regards
Ronny
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...

On Mon, 13 Oct 2008 10:19:08 -0700, Ronny <ro***@john.comwrote:

>Thanks Chris,
Looks nice but I miss the dual way communication. In the main thread to
deliver paramters and data to the worker thread- how can I do that?


It really depends on the exact data, what the thread will do with it, and
how you want the data to be delivered.

The simplest approach is to pass all the data to the thread when you start
it up. The BackgroundWorker class supports this easily. To pass data to
a thread as it''s executing means you need to come up with a specific
strategy of what data is being passed, when the thread will retrieve it,
and how. One possible approach is to use a queue that the worker thread
reads from while other threads write to (see "producer/consumer" in
Google). But that''s hardly the only approach one might take, nor is it
universally the most appropriate.

Unless you can provide a more specific question, it''s going to be very
difficult to provide any sort of answer to your question.

Pete



You could quite easily create a class which has

A: A queue<string>
B: A worker thread
C: An object you use to lock()

When you want to add data you call a public method which does

lock(SyncRoot)
Queue.Enqueue(data);
When the thread wants data

string data = null;
string hasData = false;
lock(SyncRoot)
{
hasData = Queue.Count 0;
if (hasData)
data = Queue.Dequeue();
}

//Process "data"

You can either make the thread sleep, or you can use an AutoResetEvent that
the thread waits for, and call AutoResetEvent.Set() from your public method
which enqueues.

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com


这篇关于Re:线程通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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