线程A通知事件的线程B. [英] Thread A notifying Thread B of an Event

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

问题描述

使用VS 2003,VB,MSDE ...


有两个主题,A& B,连续运行并由Sub

Main启动。他们实例化相同的代码。线程A处理电话线1上的调用

活动,线程B处理电话上的呼叫活动

第2行。它们使用通用SQL数据源,但所有DataSet都是唯一的<每个线程都有



有没有办法让线程A偶尔与线程B通信

发生了什么事?理想情况是线程A将线程B中的处理程序处理的事件提升为b $ b。但是,我没有看到怎么做

(被提升的事件将在Sub Main中处理调用堆栈,而不是&b
) ;另一个线程)。


具体来说,线程A在公共数据源中添加了一行,我需要线程B来了解。我目前正在使用线程中的计时器

B来检查公共数据源每15秒进行一次更改,这有效。

好​​的,但我正在寻找一个更简单的解决方案。我看过SQL触发器,

但是没看到它会如何警告线程B.我看过RaseEvents,

但是没有看到任何帮助还有。


有什么想法吗?做System.Timers。发挥沉重的资源使用费?如果不是我

可能会坚持使用计时器方法。


Thansk!


Bob

Using VS 2003, VB, MSDE...

There are two threads, A & B, that continously run and are started by Sub
Main. They instantiationsl of identical code. Thread A handles call
activity on telephone line 1 and Thread B handles call activity on telephone
line 2. They use a common SQL datasource, but all DataSets are unique to
each thread.

Is there a way for thread A to occasionally communication to thread B that
something has happened? The ideal situation would be for thread A to raise
an event that a handler in thread B handles. But, I don''t see how to do
that (the raised event would be handled "up the call stack" in Sub Main, not
"horizontially" by the other thread).

Specifically, thread A has added a row(s) to the common datasource that I
need thread B to know about. I am currently doing it with a timer in thread
B that check the common datasource for changes every 15 seconds, which works
OK, but am looking for a simpler solution. I have looked at SQL triggers,
but don''t see how that would alert thread B. I have looked at RaseEvents,
but don''t see any help there either.

Any ideas? Do System.Timers. exert a heavy resource usage toll? If not I
may jus stick with the timer method.

Thansk!

Bob

推荐答案



" Bob Day" <博**** @ TouchTalk.net>在消息中写道

news:ub **************** @ TK2MSFTNGP12.phx.gbl ...

"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:ub****************@TK2MSFTNGP12.phx.gbl...
使用VS 2003 ,VB,MSDE ...

有两个线程,A& B,连续运行并由Sub
Main开始。他们实例化相同的代码。线程A处理电话线1上的呼叫活动,线程B处理
电话线2上的呼叫活动。它们使用通用的SQL数据源,但所有DataSet对于每个线程都是唯一的。 />
有没有办法让线程A偶尔与线程B进行通信?
发生了什么事?理想情况是线程A到
引发线程B中的处理程序处理的事件。但是,我没有看到怎么做
那个(凸起的事件将在Sub Main中处理调用堆栈,而另一个线程则是b $ b而不是)。


你可以使用addhandler ...如果你记得你的线程创建你

有类似的东西


昏暗ThreadA作为新的System.Threading.thread(AddressOf myClass.MyFunctionA)

Dim ThreadB作为新的System.Threading.Thread(AddressOf myClass.MyFunctionB)

现在
这些行为就像任何其他类一样...这意味着你可以在线程之间进行升级和挂钩(我们很多人都是这样设计的,你认为我们怎么做

数据密集的用户界面真的很快。=))


所以,A中的所有活动都可以通过你的班级在B中获得

声明......说classA有eventA,classB有eventB ...


所以..试试这个。


sub main()


昏暗的classA作为新的myClassA

dim classB a新的myClassB


AddHandler classA.eventA,New EventHandler(AddressOf classB.onEventA)

...线程开始...


end sub


ClassB.onEventA是A类事件的匹配事件处理程序(我只是

使用无参数事件处理程序委托(那就是EventHandler是什么......)


代表们确保跨线程的沟通......(其中一个很有用

目的)


就这个而言......


HTH,

CJ

具体来说,线程A已添加公共数据源的一行,我需要线程B知道。我目前正在使用
线程B中的计时器来检查公共数据源每15秒进行一次更改,其中
工作正常,但我正在寻找更简单的解决方案。我已经查看了SQL
触发器,但是没有看到它会如何警告线程B.我看过RaseEvents,
但是也没有看到任何帮助。
<有什么想法吗?做System.Timers。发挥沉重的资源使用费?如果不是我可能会坚持使用计时器方法。

Thansk!

Bob
Using VS 2003, VB, MSDE...

There are two threads, A & B, that continously run and are started by Sub
Main. They instantiationsl of identical code. Thread A handles call
activity on telephone line 1 and Thread B handles call activity on telephone line 2. They use a common SQL datasource, but all DataSets are unique to
each thread.

Is there a way for thread A to occasionally communication to thread B that
something has happened? The ideal situation would be for thread A to raise an event that a handler in thread B handles. But, I don''t see how to do
that (the raised event would be handled "up the call stack" in Sub Main, not "horizontially" by the other thread).

You can use addhandler... If you remember from your thread creation you
have something like

Dim ThreadA as new System.Threading.thread(AddressOf myClass.MyFunctionA)
Dim ThreadB as new System.Threading.Thread(AddressOf myClass.MyFunctionB)

now these act like any other class... Which means you can raisevents and
hook between threads (a lot of us design this way, how do you think we make
data intensive UI''s so damn quick.=))

So, all those events in A are availible in B through your class
declaration... say classA has eventA and classB has eventB...

So.. lets try this.

sub main()

dim classA as new myClassA
dim classB as new myClassB

AddHandler classA.eventA, New EventHandler(AddressOf classB.onEventA)
...thread starting...

end sub

ClassB.onEventA is a matching eventhandler to the class A event (I just
used an argumentless event handler delegate (thats what EventHandler is...)

Delegates ensure communication across threads... (one of there many useful
purposes)

And thats about it...

HTH,
CJ
Specifically, thread A has added a row(s) to the common datasource that I
need thread B to know about. I am currently doing it with a timer in thread B that check the common datasource for changes every 15 seconds, which works OK, but am looking for a simpler solution. I have looked at SQL triggers, but don''t see how that would alert thread B. I have looked at RaseEvents,
but don''t see any help there either.

Any ideas? Do System.Timers. exert a heavy resource usage toll? If not I
may jus stick with the timer method.

Thansk!

Bob



感谢您的帮助,但我恐怕我需要更多的解释。我已经玩了你的评论一段时间了,并且

无法让他们以任何有意义的方式工作。


我对提升活动没有任何问题在电话中被抓到了更高的价格

堆栈。但是在第二个线程处理程序抓住的一个线程中引发事件

根本没有按预期工作。


问题:如果thead A引发了一个事件被线程B抓住,我希望

delgate在线程B上运行。但是调试暗示线程A是在线程B上运行delagate的线程A.

这是正确的吗?这会产生一系列问题。


您可以在此问题上引用网址或更详细的网址吗?我有搜索MSDN的
,并且没有提供示例代码或很好的解释。

任何类型的示例代码都可以欣赏作品。
< br $> b $ b谢谢!


鲍勃

Bob Day <博**** @ TouchTalk.net>在消息中写道

news:ub **************** @ TK2MSFTNGP12.phx.gbl ...
Thanks for your help, but I am afraid I am going to need far more
explaination. I have played with your comments for some time now, and
cannot get them to work in any meaningful way.

I have no problem with Raising an event that is caught higher up in the call
stack. But raising an event in one thread caught by a 2nd thread handler
simply isn''t working as expected.

Question: If thead A raises an event caught by thread B, I would expect the
delgate to be running on thread B . But debug inidcates that thread A is
running the delagate on thread B. Is this correct? This will present a
host of problems.

Can you reference a URL or more detailed areticles on this issue? I have
searched MSDN, and have not come up with sample code or a good explaination.
Any type of sample code the works would be appreciated.

Thanks!

Bob
"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:ub****************@TK2MSFTNGP12.phx.gbl...
使用VS 2003 ,VB,MSDE ...

有两个线程,A& B,连续运行并由Sub
Main开始。他们实例化相同的代码。线程A处理电话线1上的呼叫活动,线程B处理
电话线2上的呼叫活动。它们使用通用的SQL数据源,但所有DataSet对于每个线程都是唯一的。 />
有没有办法让线程A偶尔与线程B进行通信?
发生了什么事?理想情况是线程A到
引发线程B中的处理程序处理的事件。但是,我没有看到怎么做
那个(凸起的事件将在Sub Main中处理调用堆栈,而另一个线程则是b $ b而不是)。

具体来说,线程A在公共数据源中添加了一行,我需要线程B知道。我目前正在使用
线程B中的计时器来检查公共数据源每15秒进行一次更改,其中
工作正常,但我正在寻找更简单的解决方案。我已经查看了SQL
触发器,但是没有看到它会如何警告线程B.我看过RaseEvents,
但是也没有看到任何帮助。
<有什么想法吗?做System.Timers。发挥沉重的资源使用费?如果不是我可能会坚持使用计时器方法。

Thansk!

Bob
Using VS 2003, VB, MSDE...

There are two threads, A & B, that continously run and are started by Sub
Main. They instantiationsl of identical code. Thread A handles call
activity on telephone line 1 and Thread B handles call activity on telephone line 2. They use a common SQL datasource, but all DataSets are unique to
each thread.

Is there a way for thread A to occasionally communication to thread B that
something has happened? The ideal situation would be for thread A to raise an event that a handler in thread B handles. But, I don''t see how to do
that (the raised event would be handled "up the call stack" in Sub Main, not "horizontially" by the other thread).

Specifically, thread A has added a row(s) to the common datasource that I
need thread B to know about. I am currently doing it with a timer in thread B that check the common datasource for changes every 15 seconds, which works OK, but am looking for a simpler solution. I have looked at SQL triggers, but don''t see how that would alert thread B. I have looked at RaseEvents,
but don''t see any help there either.

Any ideas? Do System.Timers. exert a heavy resource usage toll? If not I
may jus stick with the timer method.

Thansk!

Bob



嗨Bob,


队列是跨越自由线程边界发送数据的首选方式。

有一个在线聊天对此进行了讨论:
http:// msdn .microsoft.com / chats / vstu ... dio_041602.asp


Alan Dennis的书,.NET Multithreading在解释
$ b方面表现相当不错$ b this,并提供示例代码。您可以通过 www.manning.com
> - 书籍有印刷版和电子版两种版本。


迪克

-

Richard Grier(Microsoft Visual Basic MVP)


请参阅
www.hardandsoftware.net 获取联系信息。


Visual Basic程序员串行通信指南的作者,第3版

版本ISBN 1 -890422-27-4(391页)2002年2月出版。
Hi Bob,

A queue is the preferred way to send data across free-thread boundaries.
There is an online chat that has discussion of this on:
http://msdn.microsoft.com/chats/vstu...dio_041602.asp

Alan Dennis book, .NET Multithreading does quite a good job of explaining
this, and provides example code. You can get information from the publisher
at www.manning.com -- the books is available in both print and eBook
versions.

Dick
--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer''s Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.


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

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