C#中的线程消息循环? [英] Thread Message Loop in C#?

查看:634
本文介绍了C#中的线程消息循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


我正在编写一个想要使用线程的应用程序

消息

队列,就像MFC中提供的一样。在MFC中,消息海报调用

函数PostThreadMessage。接收线程接收消息



处理它。因此,调用者和被调用者线程是不同的。我想b / b
想知道C#中是否有任何内置机制?或者我必须自己实现整个东西?


非常感谢你!

吉尔伯特

解决方案

" gilbert @ gmail" < gi ************ @ gmail.comwrote in message

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


您好!


我正在编写一个应用程序想利用一个线程

消息

队列,就像MFC中提供的一样。在MFC中,消息海报调用

函数PostThreadMessage。接收线程接收消息



处理它。因此,调用者和被调用者线程是不同的。我想b / b
想知道C#中是否有任何内置机制?或者我必须自己实现整个东西?


非常感谢!

Gilbert



假设您正在制作某种形式的WinForms应用程序,您可以在

表单上调用Invoke()。这将有效地将您传递给代表的代表传递给表单中的消息

队列,并且代理将在表单的线程上调用

而不是调用线程。


-

Adam Clauss


感谢您的回复。但是,它不是一个Windows窗体对象。

它是一个线程对象。我一直在阅读

的委托代码的beginInvoke函数。但是,它似乎只是将工作放在系统线程池中的一个线程中。我想知道是否有一个功能我可以
向我创建的线程发送消息,并且该线程的消息循环

将处理我的消息。非常感谢你!


吉尔伯特

Adam Clauss写道:


" gilbert @ Gmail和QUOT; < gi ************ @ gmail.comwrote in message

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


您好!


我正在编写一个应用程序想利用一个线程

消息

队列,就像MFC中提供的一样。在MFC中,消息海报调用

函数PostThreadMessage。接收线程接收消息



处理它。因此,调用者和被调用者线程是不同的。我想b / b
想知道C#中是否有任何内置机制?或者我必须自己实现整个东西?


非常感谢!

Gilbert



假设您正在制作某种形式的WinForms应用程序,您可以在

表单上调用Invoke()。这将有效地将您传递给代表的代表传递给表单中的消息

队列,并且代理将在表单的线程上调用

而不是调用线程。


-

Adam Clauss


Gilbert,


您可以调用Application.Run在

当前线程上安装Windows消息循环。 PostThreadMessage或Control.BeginInvoke(如果你的
有一个托管在线程上的控件)可以用来向队列发送消息



如果您不希望消息队列接收Windows消息,那么

您必须自己编写。


Brian


gilbert @ gmail写道:


你好!


我是编写一个想要使用线程的应用程序

消息

队列,就像MFC中提供的一样。在MFC中,消息海报调用

函数PostThreadMessage。接收线程接收消息



处理它。因此,调用者和被调用者线程是不同的。我想b / b
想知道C#中是否有任何内置机制?或者我必须自己实现整个东西?


非常感谢!

Gilbert

Hello!

I am writing an application that would like to make use of a thread
message
queue, like the one provided in MFC. In MFC, the message poster calls
the
function PostThreadMessage. The receiving thread receives the message
and
process it. Therefore, the caller and callee thread are different. I
would
like to know if there is any build-in mechanism in C#? Or I have to
implement the whole stuff by myself?

Thank you very much!
Gilbert

解决方案

"gilbert@gmail" <gi************@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...

Hello!

I am writing an application that would like to make use of a thread
message
queue, like the one provided in MFC. In MFC, the message poster calls
the
function PostThreadMessage. The receiving thread receives the message
and
process it. Therefore, the caller and callee thread are different. I
would
like to know if there is any build-in mechanism in C#? Or I have to
implement the whole stuff by myself?

Thank you very much!
Gilbert

Assuming you are making some form of WinForms app, you can call Invoke() on
your form. That will effectively put the delegate you pass into the message
queue of the form and the delegate will be called on the form''s thread
rather than the calling thread.

--
Adam Clauss


Thank you for your response. However, it is not a windows form object.
It is a thread object. I have been reading the beginInvoke function of
a delegate. However, it seems it just puts the work onto a thread in
the system thread pool. I wonder if there is a functionality that I can
post a message to a thread I created and a message loop of that thread
would process my message. Thank you very much!

Gilbert
Adam Clauss wrote:

"gilbert@gmail" <gi************@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...

Hello!

I am writing an application that would like to make use of a thread
message
queue, like the one provided in MFC. In MFC, the message poster calls
the
function PostThreadMessage. The receiving thread receives the message
and
process it. Therefore, the caller and callee thread are different. I
would
like to know if there is any build-in mechanism in C#? Or I have to
implement the whole stuff by myself?

Thank you very much!
Gilbert


Assuming you are making some form of WinForms app, you can call Invoke() on
your form. That will effectively put the delegate you pass into the message
queue of the form and the delegate will be called on the form''s thread
rather than the calling thread.

--
Adam Clauss


Gilbert,

You can call Application.Run to install a windows message loop on the
current thread. The PostThreadMessage or Control.BeginInvoke (if you
have a control hosted on the thread) can be used to dispatch messages
to the queue.

If you don''t want the message queue to receive windows messages then
you''ll have to write your own.

Brian

gilbert@gmail wrote:

Hello!

I am writing an application that would like to make use of a thread
message
queue, like the one provided in MFC. In MFC, the message poster calls
the
function PostThreadMessage. The receiving thread receives the message
and
process it. Therefore, the caller and callee thread are different. I
would
like to know if there is any build-in mechanism in C#? Or I have to
implement the whole stuff by myself?

Thank you very much!
Gilbert


这篇关于C#中的线程消息循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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