关于UI交互的简单线程问题 [英] Simple threading question about UI interaction

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

问题描述

我在工作线程中完成了工作,并且当工作完成时,我触发了一个事件,即
对象已订阅。该事件是否会在ui线程或工作线程中执行
处理程序代码?


我要做的是处理线程中的数据并且使用事件

更新gui。这样做是否安全?


jim

I have work being done in worker threads and I trigger an event, that gui
objects have subscribed to, when the work is complete. Will that event
handler code be executed in the ui thread or the worker thread?

What I am trying to do is process the data in a thread and use an event to
update the gui. Is it safe to do this?

jim

推荐答案

Jim,

该事件代码将在触发事件的线程中处理

(不是UI线程)。


如果你想要绕过这个,有一个实用类由

Juval Lowy编写,名为EventsHelper。您可以在以下网址找到它:

http: //www.idesign.net/idesign/uploads/EventsHelper.zip


这将允许您发起一个事件。如果你为该事件传递的代理人的目标有一个ISynchronizeInvoke的实现,它将调用

来调用该委托。所以,如果你的方法在一个从Control派生出来的类中,它将在UI线程上调用。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Jim H" <ジ** @ nospam.nospam>在消息中写道

news:uR ************** @ TK2MSFTNGP15.phx.gbl ...
Jim,

That event code will be handled in the thread that is firing the event
(not the UI thread).

If you want to get around this, there is a utility class written by
Juval Lowy called EventsHelper. You can find it at:

http://www.idesign.net/idesign/uploads/EventsHelper.zip

This will allow you to fire an event. If the target of the delegate you
pass for the event has an implementation of ISynchronizeInvoke, it will call
that to invoke the delegate. So, if your method is on a class that derives
from Control, it will be invoked on the UI thread.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jim H" <ji**@nospam.nospam> wrote in message
news:uR**************@TK2MSFTNGP15.phx.gbl...
我的工作正在完成在工作线程中,我触发了一个事件,当工作完成时,gui
对象已订阅。该事件是否会在ui线程或工作线程中执行处理程序代码?

我要做的是处理线程中的数据并使用事件来
更新gui。这样做是否安全?

jim
I have work being done in worker threads and I trigger an event, that gui
objects have subscribed to, when the work is complete. Will that event
handler code be executed in the ui thread or the worker thread?

What I am trying to do is process the data in a thread and use an event to
update the gui. Is it safe to do this?

jim



感谢您的信息。那个辅助类是用.NET 2.0编写的,而且我使用带有.NET1.1的VS2003来获得



有没有办法告诉我,我的事件处理程序,如果我在ui线程

或工作线程?如果我在我的表单代码中,我可以这样做:

ISynchronizeInvode sync = this as ISynchronizeInvoke;

if(null == sync)

{

//我在工作人员

this.Invoke(myDelegate,args);

}

else

{

//我在UI线程中

//myDelegate.DynamicInvoke(args)

}


表格是从Control实现的,它实现了ISynchronizeInvkde吗?


jim


Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:ei ************** @ tk2msftngp13.phx.gbl ...
Thanks for the info. That helper class is written in .NET 2.0 and I am
using VS2003 with .NET1.1.

Is there a way for me to tell, in my event handler, if I am in the ui thread
or a worker thread? If I am in my form code can I do this:
ISynchronizeInvode sync = this as ISynchronizeInvoke;
if(null == sync)
{
//I''m in a worker
this.Invoke(myDelegate, args);
}
else
{
//I''m in the UI thread
//myDelegate.DynamicInvoke(args)
}

The form is derived from Control which implements ISynchronizeInvkde right?

jim

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ei**************@tk2msftngp13.phx.gbl...
Jim,
该事件代码将在触发事件的线程中处理(不是UI线程)。

如果你想绕过这个,有一个实用程序由Juval Lowy撰写的课程名为EventsHelper。您可以在以下网址找到它:

http: //www.idesign.net/idesign/uploads/EventsHelper.zip

这将允许您发起一个事件。如果您为该事件传递的代理的目标具有ISynchronizeInvoke的实现,则它将调用它来调用该委托。所以,如果你的方法是在一个派生自Control的类上,它将在UI线程上调用。

希望这会有所帮助。

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

Jim H <ジ** @ nospam.nospam>在消息中写道
新闻:uR ************** @ TK2MSFTNGP15.phx.gbl ...
Jim,

That event code will be handled in the thread that is firing the event
(not the UI thread).

If you want to get around this, there is a utility class written by
Juval Lowy called EventsHelper. You can find it at:

http://www.idesign.net/idesign/uploads/EventsHelper.zip

This will allow you to fire an event. If the target of the delegate
you pass for the event has an implementation of ISynchronizeInvoke, it
will call that to invoke the delegate. So, if your method is on a class
that derives from Control, it will be invoked on the UI thread.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jim H" <ji**@nospam.nospam> wrote in message
news:uR**************@TK2MSFTNGP15.phx.gbl...
我在工作线程中完成了工作当工作完成时,我会触发一个gui
对象订阅的事件。该事件是否会在ui线程或工作线程中执行处理程序代码?

我要做的是处理线程中的数据并使用事件
更新gui。这样做是否安全?

jim
I have work being done in worker threads and I trigger an event, that gui
objects have subscribed to, when the work is complete. Will that event
handler code be executed in the ui thread or the worker thread?

What I am trying to do is process the data in a thread and use an event
to update the gui. Is it safe to do this?

jim




Jim,


代码很容易适应.NET 1.1。您不必使用

通用事件处理程序,并且可以使用您想要的任何委托。


您提供的代码也可以使用,但你必须在你的所有事件处理程序中执行

,这就像

这样的类。



" Jim H" <ジ** @ nospam.nospam>在消息中写道

新闻:ee ************** @ TK2MSFTNGP15.phx.gbl ...
Jim,

The code is easily adapted to work with .NET 1.1. You don''t have to use
the generic event handlers, and can use any delegate you want.

The code that you supplied will work as well, but you would have to do
it in all of your event handlers, which makes the case for a class like
this.


"Jim H" <ji**@nospam.nospam> wrote in message
news:ee**************@TK2MSFTNGP15.phx.gbl...
感谢您的信息。那个辅助类是用.NET 2.0编写的,我使用带有.NET1.1的VS2003。

在我的事件处理程序中,有没有办法告诉我,如果我在ui
线程还是工作线程?如果我在我的表单代码中,我可以这样做:
ISynchronizeInvode sync = this as ISynchronizeInvoke;
if(null == sync)
{
//我在一个工人
this.Invoke(myDelegate,args);
}
其他
{
//我在UI线程中
// myDelegate.DynamicInvoke(args)


表单派生自Control实现ISynchronizeInvkde
对吧?

jim
Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>在消息新闻中写道
:ei ************** @ tk2msftngp13.phx.gbl ...
Thanks for the info. That helper class is written in .NET 2.0 and I am
using VS2003 with .NET1.1.

Is there a way for me to tell, in my event handler, if I am in the ui
thread or a worker thread? If I am in my form code can I do this:
ISynchronizeInvode sync = this as ISynchronizeInvoke;
if(null == sync)
{
//I''m in a worker
this.Invoke(myDelegate, args);
}
else
{
//I''m in the UI thread
//myDelegate.DynamicInvoke(args)
}

The form is derived from Control which implements ISynchronizeInvkde
right?

jim

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:ei**************@tk2msftngp13.phx.gbl...
Jim,
该事件代码将在触发事件的线程中处理(不是UI线程)。

如果你想绕过这个,有一个由<编写的实用程序类Juval Lowy称为EventsHelper。您可以在以下网址找到它:

http: //www.idesign.net/idesign/uploads/EventsHelper.zip

这将允许您发起一个事件。如果您为该事件传递的代理的目标具有ISynchronizeInvoke的实现,则它将调用它来调用该委托。所以,如果你的方法是在一个派生自Control的类上,它将在UI线程上调用。

希望这会有所帮助。

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

Jim H <ジ** @ nospam.nospam>在消息中写道
新闻:uR ************** @ TK2MSFTNGP15.phx.gbl ...
Jim,

That event code will be handled in the thread that is firing the event
(not the UI thread).

If you want to get around this, there is a utility class written by
Juval Lowy called EventsHelper. You can find it at:

http://www.idesign.net/idesign/uploads/EventsHelper.zip

This will allow you to fire an event. If the target of the delegate
you pass for the event has an implementation of ISynchronizeInvoke, it
will call that to invoke the delegate. So, if your method is on a class
that derives from Control, it will be invoked on the UI thread.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jim H" <ji**@nospam.nospam> wrote in message
news:uR**************@TK2MSFTNGP15.phx.gbl...
我在工作线程中完成了工作当工作完成时,我会触发一个gui
对象订阅的事件。该事件是否会在ui线程或工作线程中执行处理程序代码?

我要做的是处理线程中的数据并使用事件
更新gui。这样做是否安全?

jim
I have work being done in worker threads and I trigger an event, that gui
objects have subscribed to, when the work is complete. Will that event
handler code be executed in the ui thread or the worker thread?

What I am trying to do is process the data in a thread and use an event
to update the gui. Is it safe to do this?

jim





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

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