线程之间的事件上升 [英] Rising events between threads

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

问题描述

大家好,


我最近深入研究了C#和整个.Net的东西。特别是我

发现了将事件和代表添加到C#语言中的想法非常有趣,我正试图以各种奇怪的方式使用它们

在我看来。特别是我很难找到一种方法来使用线程之间的事件




特别是我想知道如何上升和事件从一个线程和

从另一个线程中的委托收到它(我的意思是另一个

线程唤醒并执行委托)...并且继续...它是否可以播放活动?
?或者,甚至更好,决定

目标线程?


我记得过去那里有窗口中的消息,而不是

事件,你可以安全地通过线程发送它们。但他们是不同的东西,我认为,代表只是功能指针

和事件是捆绑的(好吧,可能还有一些额外的东西)...


那么可以用事件做到这一点吗?或者还有其他方式,

同样简单方便(我不是关于Monitor,Mutex和

co ...我知道他们......)?


提前致谢。再见。

Hi all,

I''ve recently digged into C# and the whole .Net stuff. Particularly I
found the idea of adding Events and Delegates to the C# language very
interesting and I''m trying to use them in every weird way that comes
to my mind. Particularly I''m struggling to find a way to use events
between threads.

In particular I''d like to know how to rise and event from a thread and
have it received from the delegate in another thread (I mean the other
thread wakes up and executes the delegate)... And going over... is it
possible to broadcast events? Or, even better, to decide the
destination thread?

I remember that in the past there where messages in windows, not
events, and you could send them through threads safely. But they were
a different thing cos, I think, delegates are just function pointers
and events are bundles of them (ok, with some extras maybe)...

So is it possible to do this with events? Or are there other ways,
similarly easy and convenient (I''m not taling about Monitor, Mutex and
co... I know them...)?

Thanks in advance. Bye.

推荐答案

你不能在没有

的情况下将一个调用注入到另一个线程的调用堆栈中做一些事情的线程被通知来电。这是

为什么你可以在UI线程上调用委托。因为UI线程有一个循环循环的消息泵,你可以注入一些东西来处理这个循环,在这种情况下,你的委托调用。


对于其他线程,你几乎必须做同样的事情,就是说,

有一个循环等待执行你的委托的信号。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


Gotch @ < da ****** @ gmail.com写信息

news:11 ********************** @ n2g2000hse .googlegro ups.com ...
You can not inject a call into the call stack of another thread without
that thread doing something to be notified of the incoming call. This is
why you can have delegates called on a UI thread. Because the UI thread has
a message pump that cycles in a loop, you can inject something to be
processed into that loop, in this case, your delegate call.

For other threads, you pretty much have to do the same thing, that is,
have a loop waiting for a signal to execute your delegate.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gotch@" <da******@gmail.comwrote in message
news:11**********************@n2g2000hse.googlegro ups.com...

大家好,


我最近深入研究了C#和整个.Net的东西。特别是我

发现了将事件和代表添加到C#语言中的想法非常有趣,我正试图以各种奇怪的方式使用它们

在我看来。特别是我很难找到一种方法来使用线程之间的事件




特别是我想知道如何上升和事件从一个线程和

从另一个线程中的委托收到它(我的意思是另一个

线程唤醒并执行委托)...并且继续...它是否可以播放活动?
?或者,甚至更好,决定

目标线程?


我记得过去那里有窗口中的消息,而不是

事件,你可以安全地通过线程发送它们。但他们是不同的东西,我认为,代表只是功能指针

和事件是捆绑的(好吧,可能还有一些额外的东西)...


那么可以用事件做到这一点吗?或者还有其他方式,

同样简单方便(我不是关于Monitor,Mutex和

co ...我知道他们......)?


提前致谢。再见。
Hi all,

I''ve recently digged into C# and the whole .Net stuff. Particularly I
found the idea of adding Events and Delegates to the C# language very
interesting and I''m trying to use them in every weird way that comes
to my mind. Particularly I''m struggling to find a way to use events
between threads.

In particular I''d like to know how to rise and event from a thread and
have it received from the delegate in another thread (I mean the other
thread wakes up and executes the delegate)... And going over... is it
possible to broadcast events? Or, even better, to decide the
destination thread?

I remember that in the past there where messages in windows, not
events, and you could send them through threads safely. But they were
a different thing cos, I think, delegates are just function pointers
and events are bundles of them (ok, with some extras maybe)...

So is it possible to do this with events? Or are there other ways,
similarly easy and convenient (I''m not taling about Monitor, Mutex and
co... I know them...)?

Thanks in advance. Bye.



谢谢,

我的问题是......我该怎么做?

On 3 Lug,17:32,Nicholas Paldino [.NET / C#MVP]"

< m ... @ spam.guard.caspershouse.comwrote:
Thanks,
and my question is... How do I do that?
On 3 Lug, 17:32, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:

你不能将调用注入到另一个线程的调用堆栈中,而没有

该线程正在做一些事情来通知来电。这是

为什么你可以在UI线程上调用委托。因为UI线程有一个循环循环的消息泵,你可以注入一些东西来处理这个循环,在这种情况下,你的委托调用。


对于其他线程,你几乎必须做同样的事情,就是说,

有一个循环等待执行你的委托的信号。


-

- Nicholas Paldino [.NET / C#MVP]

- m ... @ spam.guard.caspershouse.com


" Gotch @" < dario ... @ gmail.com写信息


新闻:11 ********************** @ n2g2000hse.googlegro ups.com ...
You can not inject a call into the call stack of another thread without
that thread doing something to be notified of the incoming call. This is
why you can have delegates called on a UI thread. Because the UI thread has
a message pump that cycles in a loop, you can inject something to be
processed into that loop, in this case, your delegate call.

For other threads, you pretty much have to do the same thing, that is,
have a loop waiting for a signal to execute your delegate.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Gotch@" <dario...@gmail.comwrote in message

news:11**********************@n2g2000hse.googlegro ups.com...

大家好,
Hi all,


我'最近我已经深入研究了C#和整个.Net的东西。特别是我

发现了将事件和代表添加到C#语言中的想法非常有趣,我正试图以各种奇怪的方式使用它们

在我看来。特别是我在努力寻找一种在线程之间使用事件的方法


I''ve recently digged into C# and the whole .Net stuff. Particularly I
found the idea of adding Events and Delegates to the C# language very
interesting and I''m trying to use them in every weird way that comes
to my mind. Particularly I''m struggling to find a way to use events
between threads.


特别是我想知道如何从一个主题上升和事件

从另一个线程中的委托(我的意思是另一个

线程唤醒并执行委托)......然后过去......是否可以广播事件?b $ b或者,更好的是,决定

目标线程?
In particular I''d like to know how to rise and event from a thread and
have it received from the delegate in another thread (I mean the other
thread wakes up and executes the delegate)... And going over... is it
possible to broadcast events? Or, even better, to decide the
destination thread?


我记得过去那里有消息在windows中,而不是
事件,你可以通过线程发送它们安全。但是他们是不同的东西,我认为,代表只是功能指针

和事件是捆绑的(好吧,可能还有一些额外的东西)...
I remember that in the past there where messages in windows, not
events, and you could send them through threads safely. But they were
a different thing cos, I think, delegates are just function pointers
and events are bundles of them (ok, with some extras maybe)...


那么可以用事件做到这一点吗?或者还有其他方式,

同样简单方便(我不是关于Monitor,Mutex和

co ...我知道他们......)?
So is it possible to do this with events? Or are there other ways,
similarly easy and convenient (I''m not taling about Monitor, Mutex and
co... I know them...)?


先谢谢。再见。
Thanks in advance. Bye.



就像我告诉你的那样,你基本上必须在你的主题中有一个循环

将处理在其他线程上设置的消息/通知。

您可以使用Windows消息,某种队列结构等等。这是

a基本的生产者/消费者模式。


是否有一个特殊原因,你想拥有一个单独的线程

流程代表?你究竟想做什么?

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Gotch @" < da ****** @ gmail.com写信息

news:11 ********************** @ m36g2000hse .googlegr oups.com ...
Just like I told you, you basically have to have a loop in your thread
which will process messages/notifications which are set on other threads.
You can use windows messages, some sort of queue structure, etc, etc. It''s
a basic producer/consumer pattern.

Is there a particular reason why you want to have one single thread
process delegates? What exactly are you trying to do?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gotch@" <da******@gmail.comwrote in message
news:11**********************@m36g2000hse.googlegr oups.com...

谢谢,

我的问题是......我该怎么办?


On 3 Lug,17:32,Nicholas Paldino [.NET / C#MVP]

< m ... @ spam.guard.caspershouse .comwrote:
Thanks,
and my question is... How do I do that?
On 3 Lug, 17:32, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:

>您不能将呼叫注入另一个线程的调用堆栈中,而不是该线程正在做一些事情以通知来电。这就是为什么你可以在UI线程上调用委托的原因。因为UI线程有一个循环循环的消息泵,你可以将一些东西注入到该循环中,在这种情况下是你的委托调用。
<对于其他线程,你几乎必须做同样的事情,
是,有一个循环等待信号来执行你的代表。

-
- Nicholas Paldino [.NET / C#MVP]
- m ... @ spam.guard.caspershouse.com

Gotch @ < dario ... @ gmail.com写信息

新闻:11 ********************** @ n2g2000hse.googlegr oups .com ...
> You can not inject a call into the call stack of another thread
without
that thread doing something to be notified of the incoming call. This is
why you can have delegates called on a UI thread. Because the UI thread
has
a message pump that cycles in a loop, you can inject something to be
processed into that loop, in this case, your delegate call.

For other threads, you pretty much have to do the same thing, that
is,
have a loop waiting for a signal to execute your delegate.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Gotch@" <dario...@gmail.comwrote in message

news:11**********************@n2g2000hse.googlegr oups.com...

大家好,
Hi all,


我最近挖了进入C#和整个.Net的东西。特别是我

发现了将事件和代表添加到C#语言中的想法非常有趣,我正试图以各种奇怪的方式使用它们

在我看来。特别是我在努力寻找一种在线程之间使用事件的方法


I''ve recently digged into C# and the whole .Net stuff. Particularly I
found the idea of adding Events and Delegates to the C# language very
interesting and I''m trying to use them in every weird way that comes
to my mind. Particularly I''m struggling to find a way to use events
between threads.


特别是我想知道如何从一个主题上升和事件

从另一个线程中的委托(我的意思是另一个

线程唤醒并执行委托)......然后过去......是否可以广播事件?b $ b或者,更好的是,决定

目标线程?
In particular I''d like to know how to rise and event from a thread and
have it received from the delegate in another thread (I mean the other
thread wakes up and executes the delegate)... And going over... is it
possible to broadcast events? Or, even better, to decide the
destination thread?


我记得过去那里有消息在windows中,而不是
事件,你可以通过线程发送它们安全。但是他们是不同的东西,我认为,代表只是功能指针

和事件是捆绑的(好吧,可能还有一些额外的东西)...
I remember that in the past there where messages in windows, not
events, and you could send them through threads safely. But they were
a different thing cos, I think, delegates are just function pointers
and events are bundles of them (ok, with some extras maybe)...


那么可以用事件做到这一点吗?或者还有其他方式,

同样简单方便(我不是关于Monitor,Mutex和

co ...我知道他们......)?
So is it possible to do this with events? Or are there other ways,
similarly easy and convenient (I''m not taling about Monitor, Mutex and
co... I know them...)?


先谢谢。再见。
Thanks in advance. Bye.




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

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