如何将代码编组到原始线程 [英] How to marshal code to the original thread

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

问题描述

我有一个处理来自另一个类的事件的类。不幸的是

这些事件是在不同的线程上到达的。我如何编写代码

执行到创建调用者类的线程。


''示例


公共类MyClass

{

private ClassWithEvents cls = new ClassWithEvents();

public MyClass()

{

cls.Hangup + = new HangupEventHandler(cls_Hangup);

}

private void cls_Hangup(object sender,HangupEvent e)

{

//这发生在另一个线程上

//它需要被编组到原始线程。

}

}

I have a class that handles events from another class. Unfortunately
these events arrive on a different thread. How can I marshal the code
execution to the thread on which the caller class was created.

''example

public class MyClass
{
private ClassWithEvents cls = new ClassWithEvents();
public MyClass()
{
cls.Hangup += new HangupEventHandler(cls_Hangup);
}
private void cls_Hangup(object sender, HangupEvent e)
{
// this happens on a different thread
// it needs to be marshalled to the original thread.
}
}

推荐答案

弗兰克,


你可以'除非您要编组的线程是UI线程。如果它是

你可以使用Control.Invoke或Control.BeginInvoke来创建线程中创建的一些控件



-


Stoitcho Goutsev(100)


新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
Frank,

You can''t unless the thread you want to marshal to is an UI thread. If it is
you can use Control.Invoke or Control.BeginInvoke for some of the controls
created in the thread.
--

Stoitcho Goutsev (100)

"Frank Rizzo" <no**@none.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
我有处理来自另一个类的事件的类。不幸的是,这些
事件到达了另一个线程。如何编写代码执行到创建调用者类的线程。

''示例

公共类MyClass
{<私有ClassWithEvents cls = new ClassWithEvents();
public MyClass()
{
cls.Hangup + = new HangupEventHandler(cls_Hangup);
}

private void cls_Hangup(对象发送者,HangupEvent e)
{
//这发生在另一个线程上
//需要编组到原始线程。
}
I have a class that handles events from another class. Unfortunately these
events arrive on a different thread. How can I marshal the code execution
to the thread on which the caller class was created.

''example

public class MyClass
{
private ClassWithEvents cls = new ClassWithEvents();
public MyClass()
{
cls.Hangup += new HangupEventHandler(cls_Hangup);
}
private void cls_Hangup(object sender, HangupEvent e)
{
// this happens on a different thread
// it needs to be marshalled to the original thread.
} }



Stoitcho,


嗯,这不完全正确。您可以在非UI线程上执行此操作,

但它必须是自定义机制,并且线程必须是处理某种类型的消息的
循环(就像windows消息一样)。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Stoitcho Goutsev(100)" < 10*@100.com>在消息中写道

news:ua ************* @ TK2MSFTNGP09.phx.gbl ...
Stoitcho,

Well, that''s not completely true. You could do it on a non UI thread,
but it would have to be a custom mechanism, and the thread would have to be
processing messages of some sort in a loop (like windows messages do).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Stoitcho Goutsev (100)" <10*@100.com> wrote in message
news:ua*************@TK2MSFTNGP09.phx.gbl...
Frank,

除非您想要编组的线程是UI线程,否则你不能这样做。如果它是
你可以使用Control.Invoke或Control.BeginInvoke来创建线程中创建的一些
控件。

-

Stoitcho Goutsev(100)

Frank Rizzo <无** @ none.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
Frank,

You can''t unless the thread you want to marshal to is an UI thread. If it
is you can use Control.Invoke or Control.BeginInvoke for some of the
controls created in the thread.
--

Stoitcho Goutsev (100)

"Frank Rizzo" <no**@none.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
我有一个处理的类另一个班级的事件。不幸的是
这些事件是在不同的线程上到达的。如何将代码执行编组到创建调用者类的线程上。

''示例

公共类MyClass
{<私有ClassWithEvents cls = new ClassWithEvents();
public MyClass()
{
cls.Hangup + = new HangupEventHandler(cls_Hangup);
}

private void cls_Hangup(对象发送者,HangupEvent e)
{
//这发生在另一个线程上
//需要编组到原始线程。
}
I have a class that handles events from another class. Unfortunately
these events arrive on a different thread. How can I marshal the code
execution to the thread on which the caller class was created.

''example

public class MyClass
{
private ClassWithEvents cls = new ClassWithEvents();
public MyClass()
{
cls.Hangup += new HangupEventHandler(cls_Hangup);
}
private void cls_Hangup(object sender, HangupEvent e)
{
// this happens on a different thread
// it needs to be marshalled to the original thread.
} }




Nicholas Paldino [.NET / C#MVP]写道:
Nicholas Paldino [.NET/C# MVP] wrote:
Stoitcho,

嗯,这不完全正确。您可以在非UI线程上执行此操作,但它必须是自定义机制,并且线程必须在循环中处理某种类型的消息(如Windows消息一样)。
Stoitcho,

Well, that''s not completely true. You could do it on a non UI thread,
but it would have to be a custom mechanism, and the thread would have to be
processing messages of some sort in a loop (like windows messages do).



我对此持开放态度。你有什么样的例子吗?


问候


I am open to that. Would you have some type of an example?

Regards


这篇关于如何将代码编组到原始线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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