线程和代表 [英] Threads and Delegates

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

问题描述

关于线程和代表的快速提问。


我有以下场景


线程A(CLASSA)产生线程B(CLASSB)并将一个DelegateA传递给

回调

线程B异步调用DelegateB(可能是一个计时器,但我们

得到线程C)

完成DelegateB后,线程B想要使用

DelegateA回调ThreadA,但我们都知道对DelegateA的调用是在ThreadB中运行的。无论如何都是

让线程B调用线程A中的方法。


我的问题是我需要通知一个有效的单线程COM对象

已经完成。我可以通过向该COM对象引发一个事件来做到这一点,但是如果该对象试图使用他当前拥有的任何指针然后POW(

是有意义的话),那么就是
。 br />

我在C ++中编写了一些非常类似的东西,我想知道这是否可以从VB.NET完成?b
$ b否则我只需将我的C ++代码移植到V7。


谢谢,


Lior


解决方案

一个更简单的路线可能只是让threadB引发一个即使它是什么时候

完成了你做的任何你做的事情。这样维护代码

会更容易,因为threadA负责创建threadB,

在threadA中分配一个委托来处理done() threadB中的事件,以及用于撕掉threadB的



考虑合并。


hth,


史蒂夫

Lior Amar <李******* @ hotmail.com>写在消息

新闻:ey ************** @ TK2MSFTNGP12.phx.gbl ...

关于线程的快速问题代表们。

我有以下场景

线程A(CLASSA)生成线程B(CLASSB)并将DelegateA传递给
回调
线程B异步调用DelegateB(可能是一个计时器但我们得到线程C)
完成DelegateB后,线程B想使用DelegateA回调ThreadA
,但我们都知道对DelegateA的调用正在ThreadB中运行。
无论如何都要让线程B调用线程A中的方法。

我的问题是我需要通知一个已经完成工作的单线程COM对象。我可以通过向该COM对象
引发一个事件来做到这一点但是如果该Object试图使用他当前拥有的任何指针然后
POW(这是有意义的)。

我用C ++编写了一些非常类似的东西,我想知道这是否可以从VB.NET中完成?否则我只需将我的C ++代码移植到V7。

谢谢,



Lior,

关于线程和代表的快速问题。

我有以下场景

线程A( CLASSA)生成线程B(CLASSB)并将其传递给一个DelegateA到
回调
线程B异步调用DelegateB(可能是一个计时器但我们将获得线程C)
完成DelegateB后,线程B想使用
DelegateA回调ThreadA,但我们都知道对DelegateA的调用是在ThreadB中运行的。无论如何都要让线程B调用线程A中的方法。




线程B除了调用委托A之外还做了什么吗?如果没有,为什么

你不是直接从线程A做的吗?


如果那是不可能的,但是线程A是一个GUI线程,你可以

考虑在该线程上创建的某些控件上使用Control.Invoke。

如果它不是UI线程,可能使用AutoResetEvent或

ManualResetEvent会更好。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net / dotnet /

请仅回复新闻组。


将事件从线程B提升回线程A是等效的委托代表

。该活动将被CLASSA捕获,但它将在线程B上。

我的挑战仍然是重新进入线程B.


这是一个我试过的第一件事......难道不是那么好吗

就这么简单:)我已经在这2天了,现在试图找出

如何重新输入主题A.


感谢您的帮助,


Lior


" steve" < as*@abc.com>在消息中写道

news:vn ************ @ corp.supernews.com ...

一条更简单的路线可能就是有线程B在完成你做的任何事情时完成任务。这样维护
代码会更容易,因为threadA负责创建threadB,
在threadA中分配一个委托来处理done() threadB中的事件,
以及用于撕掉threadB的事件。

考虑合并。

hth,

史蒂夫

Lior Amar <李******* @ hotmail.com>在消息中写道
新闻:ey ************** @ TK2MSFTNGP12.phx.gbl ...

关于线程和代表的快速提问。 />
我有以下场景

线程A(CLASSA)产生线程B(CLASSB)并将其传递给
a回调
线程B调用DelegateB异步(可能是一个计时器但接着
我们得到线程C)
完成DelegateB后,线程B想要使用

DelegateA来回调ThreadA,但我们都知道这个电话委托A在
ThreadB中运行。无论如何

让线程B调用线程A中的方法。

我的问题是我需要通知单个线程的COM对象



工作已经完成。我可以通过向该COM对象引发事件来实现此目的


但是

如果该Object尝试使用他当前拥有的任何指针


POW(

有意义)。

我在C ++中编写了非常类似的东西,我想知道这是否可以从VB.NET完成?否则我只需将我的C ++代码移植到V7。

谢谢,




Quick question about threads and delegates.

I have the following scenario

Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a
callback
Thread B Invokes a DelegateB asynchronously (could be a timer but then we
get Thread C)
Upon completion of DelegateB, Thread B would like to callback ThreadA using
DelegateA but as we all know the call to DelegateA is running in ThreadB. Is
there anyway to get Thread B to call a method in Thread A.

My Problem is that I need to notify a single threaded COM object that work
has been completed. I can do this by raising an event to that COM object but
if that Object tries to use any pointer that he currently has then POW(which
makes sense).

I''ve coded something very similar in C++ and am wondering if this can be
done from VB.NET? Otherwise I''ll just port my C++ code to V7.

Thanks,

Lior


解决方案

an easier route may just be to have threadB raise an even when it is
finished doing whatever you created it to do. that way maintaining the code
would be a bit easier as threadA is responsible for creating threadB,
assigning a delegate in threadA to handle the "done()" event in threadB, and
for tearing threadB down.

think about consolidating.

hth,

steve
"Lior Amar" <li*******@hotmail.com> wrote in message
news:ey**************@TK2MSFTNGP12.phx.gbl...

Quick question about threads and delegates.

I have the following scenario

Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a
callback
Thread B Invokes a DelegateB asynchronously (could be a timer but then we
get Thread C)
Upon completion of DelegateB, Thread B would like to callback ThreadA using DelegateA but as we all know the call to DelegateA is running in ThreadB. Is there anyway to get Thread B to call a method in Thread A.

My Problem is that I need to notify a single threaded COM object that work
has been completed. I can do this by raising an event to that COM object but if that Object tries to use any pointer that he currently has then POW(which makes sense).

I''ve coded something very similar in C++ and am wondering if this can be
done from VB.NET? Otherwise I''ll just port my C++ code to V7.

Thanks,

Lior



Lior,

Quick question about threads and delegates.

I have the following scenario

Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a
callback
Thread B Invokes a DelegateB asynchronously (could be a timer but then we
get Thread C)
Upon completion of DelegateB, Thread B would like to callback ThreadA using
DelegateA but as we all know the call to DelegateA is running in ThreadB. Is
there anyway to get Thread B to call a method in Thread A.



Does Thread B do anything else but invoke Delegate A? If not, why
don''t you do it directly from Thread A?

If that''s not possible, but Thread A is a GUI thread, you could
consider using Control.Invoke on some control created on that thread.
If it''s not a UI thread, perhaps using an AutoResetEvent or
ManualResetEvent would be better.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.


Raising an event back to Thread A from Thread B is the equivalent of having
the delegate. The event will be caught by CLASSA but it will be on Thread B.
My challenged then is still to re-enter Thread B.

It was one of the first thing I tried...Wouldn''t it have been so nice to be
that simple :) I''ve been going at this for 2 days now trying to figure out
how to re-enter thread A.

Thanks for the help though,

Lior

"steve" <as*@abc.com> wrote in message
news:vn************@corp.supernews.com...

an easier route may just be to have threadB raise an even when it is
finished doing whatever you created it to do. that way maintaining the code would be a bit easier as threadA is responsible for creating threadB,
assigning a delegate in threadA to handle the "done()" event in threadB, and for tearing threadB down.

think about consolidating.

hth,

steve
"Lior Amar" <li*******@hotmail.com> wrote in message
news:ey**************@TK2MSFTNGP12.phx.gbl...

Quick question about threads and delegates.

I have the following scenario

Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a callback
Thread B Invokes a DelegateB asynchronously (could be a timer but then we get Thread C)
Upon completion of DelegateB, Thread B would like to callback ThreadA using

DelegateA but as we all know the call to DelegateA is running in ThreadB. Is

there anyway to get Thread B to call a method in Thread A.

My Problem is that I need to notify a single threaded COM object that


work has been completed. I can do this by raising an event to that COM object


but

if that Object tries to use any pointer that he currently has then


POW(which

makes sense).

I''ve coded something very similar in C++ and am wondering if this can be
done from VB.NET? Otherwise I''ll just port my C++ code to V7.

Thanks,

Lior




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

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