你能提出一个不阻止的事件吗? [英] Can you raise an event that doesn't block?

查看:57
本文介绍了你能提出一个不阻止的事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,您通常希望在相同的

线程中引发一个事件,以便您的代码阻止,直到事件被处理完毕...

但是我遇到过一个我正在运行线程的情况

不是我的表格线程我想让它提出一个自定义事件并继续

去无需等待处理该事件...


这一切都可能吗?

I understand that you would normally want to raise an event on the same
thread so that your code blocks until the event has been dealt with...
however i''ve run into a situation where I have a thread running that
isn''t my form thread and I want it to raise a custom event and continue
going without waiting for that event to be handled...

Is this at all possible?

推荐答案

班尼,


当然。只需在事件上使用BeginInvoke方法,该方法将生成一个

线程池线程来为此调用提供服务,并且您的线程将继续以

结婚方式运行。请记住,如果事件内的

调用列表中有多个项目(请参阅事件的GetInvocationList)

,您必须遍历它们并且每个人都打电话给BeginInvoke。


Dave


" Benny Raymond" <是*** @ pocketrocks.com>在留言中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
Benny,

Sure. Just use the BeginInvoke method on the event, which will spawn off a
thread pool thread to service this call, and your thread will go on its
marry way. Just remember that if there are more than one items in the
invocation list inside of the event (see GetInvocationList of the event)
that you have to iterate through them all and call BeginInvoke on each.

Dave

"Benny Raymond" <be***@pocketrocks.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
我明白了您通常希望在相同的线程上引发一个事件,以便您的代码阻塞,直到事件被处理完......
但是我遇到了我有一个线程的情况运行那不是我的表单线程,我希望它提出一个自定义事件并继续前进
而不等待该事件被处理......

这是在所有可能的情况下?
I understand that you would normally want to raise an event on the same
thread so that your code blocks until the event has been dealt with...
however i''ve run into a situation where I have a thread running that isn''t
my form thread and I want it to raise a custom event and continue going
without waiting for that event to be handled...

Is this at all possible?



这似乎有效 - 但是因为我不太了解它我会

喜欢你看代码并让我知道我做对了...谢谢!


//我的活动事件

#region代表和活动声明> MessageReceived事件

public delegate void MessageReceivedEventHandler(ServerMessage sMsg);

公共事件MessageReceivedEventHandler MessageReceived;

private void OnMessageReceived(ServerMessage sMsg)

{

if(MessageReceived!= null)

{

IEnumerator myEnum = MessageReceived.GetInvocationList()。GetEnumerator() ;

while(myEnum.MoveNext())

{

((MessageReceivedEventHandler)myEnum.Current).BeginInvoke(sMsg,null,

null);

}

}

}

#endregion


//在循环中循环的线程内,

//如果在此循环中收到消息,则运行此行:

OnMessageReceived(消息);


D. Yates写道:
It seems to be working - but since I don''t understand it that much I''d
like for you to look at the code and let me know I did it right... thanks!

// My event thing
#region Delegates and Event Declarations > MessageReceived Event
public delegate void MessageReceivedEventHandler( ServerMessage sMsg );
public event MessageReceivedEventHandler MessageReceived;
private void OnMessageReceived( ServerMessage sMsg )
{
if ( MessageReceived != null )
{
IEnumerator myEnum = MessageReceived.GetInvocationList().GetEnumerator( );
while ( myEnum.MoveNext() )
{
( (MessageReceivedEventHandler) myEnum.Current ).BeginInvoke(sMsg, null,
null);
}
}
}
#endregion

// Inside the thread that loops round and round,
// if a message is recieved in this loop, it runs this line:
OnMessageReceived( message );


D. Yates wrote:
Benny,

当然。只需在事件上使用BeginInvoke方法,该方法将生成一个线程池线程来为此调用提供服务,并且您的线程将以其结婚方式进行。请记住,如果事件内的
调用列表中有多个项目(请参阅事件的GetInvocationList)
您必须遍历它们并在每个项目上调用BeginInvoke。

Dave

Benny Raymond <是*** @ pocketrocks.com>在消息中写道
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
Benny,

Sure. Just use the BeginInvoke method on the event, which will spawn off a
thread pool thread to service this call, and your thread will go on its
marry way. Just remember that if there are more than one items in the
invocation list inside of the event (see GetInvocationList of the event)
that you have to iterate through them all and call BeginInvoke on each.

Dave

"Benny Raymond" <be***@pocketrocks.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
我明白了您通常希望在相同的线程上引发一个事件,以便您的代码阻塞,直到事件被处理完......
但是我遇到了我有一个线程的情况运行那不是我的表单线程,我希望它提出一个自定义事件并继续前进
而不等待该事件被处理......

这是在所有可能的情况下?
I understand that you would normally want to raise an event on the same
thread so that your code blocks until the event has been dealt with...
however i''ve run into a situation where I have a thread running that isn''t
my form thread and I want it to raise a custom event and continue going
without waiting for that event to be handled...

Is this at all possible?




>请记住,如果
事件内部的调用列表(参见事件的GetInvocationList)
你必须遍历它们并在每个上调用BeginInvoke。
invocation list inside of the event (see GetInvocationList of the event)
that you have to iterate through them all and call BeginInvoke on each.



我想你不要除非你想要_each_注册处理程序

在自己的线程上运行,否则不需要它。通常,在事件

上调用BeginInvoke就足够了,所有注册的处理程序将在同一个单独的线程上被调用




Thi


I think you don''t need that unless you want _each_ registered handler
to be run on its own thread. Normally, calling BeginInvoke on the event
is adequate, and all the registered handlers will be invoked
sequecially on a same separate thread.

Thi


这篇关于你能提出一个不阻止的事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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