线程问题 [英] Threading questions

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

问题描述

我有一个简单的需求,我似乎无法找到文档中的答案。

大多数示例显示工作线程如何将数据传递回

创建了它。


我需要做相反的事情。

假设工作线程显示一个表单和创建它的线程

偶尔想要更改该表格的文字。


无法找到一种简单的方法。

提前致谢

I have a simple need that I can''t seem to locate the answer to in the docs.
Most examples show how a worker thread can pass data back to the thread that
created it.

I need to do the opposite.
Suppose the worker thread displays a form and the thread that created it
wants to change that form''s text occasionally.

Can''t seem to locate a simple way of doing that.

Thanks in advance

推荐答案

希望这有帮助......

示例在C ++中,但概念应该在VB中相同

http://www.codeproject.com/managedcpp/mcppthreads01.asp


**开发人员**写道:
Hope this helps....
The example is in C++ but the concept should be the same in VB

http://www.codeproject.com/managedcpp/mcppthreads01.asp

**Developer** wrote:
我有一个简单的需要,我似乎无法在文档中找到答案。
大多数例子都显示工作线程如何将数据传递回创建它的线程。

我需要反其道而行。
假设工作线程显示一个表单和创建的线程它偶尔想要改变那个形式的文本。

似乎无法找到一种简单的方法。

提前致谢
I have a simple need that I can''t seem to locate the answer to in the docs.
Most examples show how a worker thread can pass data back to the thread that
created it.

I need to do the opposite.
Suppose the worker thread displays a form and the thread that created it
wants to change that form''s text occasionally.

Can''t seem to locate a simple way of doing that.

Thanks in advance



我没有遇到启动线程并传递参数的问题。

它是'在线程启动之后我需要将数据传递给它 - 这就是我

我不知道该怎么做。

感谢您的回复
" Ernie Otero" < EO ********** @ dsli.com>在留言中写道

news:8 _ ******************** @ dsli.com ...
I don''t have a problem starting a thread and passing it parameters.
It''s after the thread is started I need to pass data to it - that''s what I
don''t know how to do.
Thanks for replying
"Ernie Otero" <eo**********@dsli.com> wrote in message
news:8_********************@dsli.com...
希望这有帮助....
示例在C ++中,但概念在VB中应该是相同的

http://www.codeproject.com/managedcpp/mcppthreads01.asp

**开发者**写道:
Hope this helps....
The example is in C++ but the concept should be the same in VB

http://www.codeproject.com/managedcpp/mcppthreads01.asp

**Developer** wrote:
我有一个简单的需求,我似乎无法在
文档中找到答案。
大多数示例显示工作线程如何将数据传递回线程<创建它。

我需要做相反的事情。
假设工作线程显示一个表单,创建它的线程
想要改变那个表单''偶尔发表文字。

无法找到一种简单的方法。

提前致谢
I have a simple need that I can''t seem to locate the answer to in the
docs.
Most examples show how a worker thread can pass data back to the thread
that created it.

I need to do the opposite.
Suppose the worker thread displays a form and the thread that created it
wants to change that form''s text occasionally.

Can''t seem to locate a simple way of doing that.

Thanks in advance






|假设工作线程显示一个表单和创建它的线程

|我想偶尔更改那个表单的文本。

如果工作线程正在显示一个表单,那么你可以在

上使用Control.Invoke表单或其中一个表单控制将信息传递给工人

线程。


或者如果工作线程没有表格,我使用了

System.Collections.Queue将信息发送给线程。线程将

在队列中寻找信息&对它进行操作。


类似(VS 2003语法):


''未经测试,从内存中输入。

类公共
/>
私人只读m_padlock作为新对象

私人只读m_queue作为新队列

私人只读m_event作为新AutoResetEvent(False)


''从主线程调用

Public Sub AddRequest(ByVal request As Object)

SyncLock m_padlock

m_queue.Enqueue(对象)

结束SyncLock

m_event.Set()

End Sub


''从工人线程调用

公共函数GetRequest()作为对象

''检查是否已有项目可用

SyncLock m_padlock

如果m_queue.Count()> 0然后

返回m_queue.DeQueue()

结束如果

结束SyncLock


''无法阻止工作线程

''在等待主线程添加请求时

m_event.WaitOne()


''必须有一个项目

SyncLock m_padlock

返回m_queue.Dequeue()

结束SyncLock

结束函数


结束类


队列用于将请求从主线程发送到Worker

线程。 m_padlock用于保护Queue.Enqueue& Queue.Dequeue

方法。工作者线程进入睡眠状态。如果要处理的

队列中没有项目,则AutoResetEvent用于通知(唤醒)工作人员

线程有更多项目可用。


在VS 2005(.NET 2.0)中,我会考虑使用

System.Collections.Generic.Queue(Of T)而不是基于对象的Queue

以上:
http:/ /msdn2.microsoft.com/en-us/library/7977ey2c.aspx


-

希望这会有所帮助

Jay [MVP - Outlook]

..NET应用程序架构师,爱好者,&福音传教士

T.S.布拉德利 - http://www.tsbradley.net

" ; ** **开发" < RE ************* @ a-znet.com>在消息中写道

新闻:大江************* @ TK2MSFTNGP11.phx.gbl ...

|我有一个简单的需要我无法在文档中找到答案。

|大多数示例显示了一个工作线程如何将数据传递回线程



|创造了它。

|

|我需要做相反的事情。

|假设工作线程显示一个表单和创建它的线程

|想偶尔更改那个表格的文字。

|

|

|

|似乎无法找到一种简单的方法。

|

|

|

|提前致谢

|

|

|
| Suppose the worker thread displays a form and the thread that created it
| wants to change that form''s text occasionally.
If the worker thread is showing a Form, then you can use Control.Invoke on
that form or one of its control to transfer information to the worker
thread.

Alternatively if the worker thread does not have a Form, I have used a
System.Collections.Queue to send information to a thread. The thread would
look for information in the queue & operate on it.

Something like (VS 2003 syntax):

'' untested, typed from memory.
Public Class ThreadRequestQueue

Private Readonly m_padlock As New Object
Private Readonly m_queue As New Queue
Private Readonly m_event As New AutoResetEvent(False)

'' called from the Main thread
Public Sub AddRequest(ByVal request As Object)
SyncLock m_padlock
m_queue.Enqueue(Object)
End SyncLock
m_event.Set()
End Sub

'' called from the Worker thread
Public Function GetRequest() As Object
'' Check to see if there are already items available
SyncLock m_padlock
If m_queue.Count() > 0 Then
Return m_queue.DeQueue()
End If
End SyncLock

'' Cannot block worker thread
'' while waiting for the main thread to add requests
m_event.WaitOne()

'' There must be an item
SyncLock m_padlock
Return m_queue.Dequeue()
End SyncLock
End Function

End Class

The Queue is used to send the requests from the Main thread to the Worker
thread. The m_padlock is used to protect the Queue.Enqueue & Queue.Dequeue
methods. The worker thread "goes to sleep" if there are no items in the
queue to work on, the AutoResetEvent is used to notify (wake up) the worker
thread there are more items available.

In VS 2005 (.NET 2.0) I would consider using a
System.Collections.Generic.Queue(Of T) instead of the object based Queue
above:
http://msdn2.microsoft.com/en-us/library/7977ey2c.aspx

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
" **Developer**" <RE*************@a-znet.com> wrote in message
news:Oe*************@TK2MSFTNGP11.phx.gbl...
|I have a simple need that I can''t seem to locate the answer to in the docs.
| Most examples show how a worker thread can pass data back to the thread
that
| created it.
|
| I need to do the opposite.
| Suppose the worker thread displays a form and the thread that created it
| wants to change that form''s text occasionally.
|
|
|
| Can''t seem to locate a simple way of doing that.
|
|
|
| Thanks in advance
|
|
|


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

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