在主/子线程之间使用委托 [英] Using delegates between main/sub threads

查看:149
本文介绍了在主/子线程之间使用委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


这是我面临的一个问题(这可能太简单了,但后来我

承认我不是大师: - )


我有一个主线程,在托管C ++中,处理显示

表格和一些控件。


我从这个线程调用另一个线程进行一些处理,所以

基本上主线程在子线程上等待。


现在sub -thread必须继续将其进度发布到主要的
线程。我有一个委托机制。所以使用互斥锁,我在子线程的范围内更新进度变量,然后调用

主线程的委托方法来显示进度。


问题就在于调用委托时。控制开关

形成子线程到主线程。但主线程已经在等待

子线程完成,所以它就在那里死锁。


任何想法/建议?解决方案看起来很简单,只是我还没有获得

它。


谢谢!

解决方案

Pixel.to.life< pi *********** @ gmail.comwrote:


这是一个问题我面临的问题(可能太简单了,但接着我是

承认我不是大师:-)


我有一个主线程,在托管中C ++,它处理显示一个

表格和一些控件。


我从这个线程调用另一个线程进行一些处理,所以

基本上主线程在子线程上等待。



这是你的问题 - 不要让主线程等待。

使用其他线程在UI应用程序中工作的重点是避免UI锁定。你需要保持UI事件循环免费处理事情

就像屏幕刷新一样。


-

Jon Skeet - < ; sk *** @ pobox.com>

网站: http://www.pobox.com/~skeet

博客: http://www.msmvps.com/jon.skeet

C#深度: http://csharpindepth.com


5月6日,1:21 * pm,Jon Skeet [C#MVP ]< sk ... @ pobox.comwrote:


Pixel.to.life< pixel.to.l ... @ gmail.comwrote:


这是我面临的一个问题(可能太简单了,但后来我

承认我不是大师:-)


我在托管C ++中有一个处理displayi的主线程一个

表格和一些控件。


我从这个线程调用另一个线程进行一些处理,所以

基本上主线程在子线程上等待。



这是你的问题 - 不要让主线程等待。

使用其他线程在UI应用程序中工作的重点是避免UI锁定。你需要保持UI事件循环免费处理事情

就像屏幕刷新一样。


-

Jon Skeet - < ; sk ... @ pobox.com>

网站: http://www.pobox.com/~skeet*

博客: http://www.msmvps.com/jon.skeet

C#深度: http://csharpindepth.com



谢谢Jon。


我我发布后意识到这一点。


现在我使用WaitForSingleThread(..)API在子线程上等待。

什么是替代方案所以子线程可以异步处理
吗?


我正在查看MsgWaitForMultipleObjects(..)而不是看看我能不能获得
状态就好了。


对不起,我是GUI多线程的新手。

5月6日,1:21 * pm,Jon Skeet [C#MVP]< sk ... @ pobox.comwrote:


Pixel.to.life< pixel.to.l ... @ gmail.comwrote:


这是我面临的问题(可能也是很简单,但后来我

承认我不是大师:-)


我有一个主线程,在托管中C ++,用于处理显示

表单和一些控件。


我从这个线程调用另一个线程进行一些处理,所以

基本上主线程在子线程上等待。



这是你的问题 - 不要让主线程等待。

使用其他线程在UI应用程序中工作的重点是避免UI锁定。你需要保持UI事件循环免费处理事情

就像屏幕刷新一样。


-

Jon Skeet - < ; sk ... @ pobox.com>

网站: http://www.pobox.com/~skeet*

博客: http://www.msmvps.com/jon.skeet

C#深度: http://csharpindepth.com



另外,我需要等待子线程,因为主线程必须

在子线程完成后做一些善意。如果我删除

WaitForSingleThread(...)调用,我怎么知道子线程已经完成?


谢谢。


Dear All,

Here is a problem I am facing (it might be too simple, but then I
admit I am not a Guru:-)

I have a main thread, in managed C++, that deals with displaying a
form and some controls.

I invoke another thread for some processing from this thread, so
basically the main thread is waiting on the sub thread.

Now the sub-thread has to keep posting its progress to the main
thread. I have a delegate mechanism for that. So using mutex, I update
the progress variable in the sub-thread''s scope, and then invoke the
main thread''s delegate method to display the progress.

The problem is just when the delegate is invoked. Control switches
form sub to main thread. But main thread is already waiting for the
sub thread to finish, so it deadlocks right there.

Any ideas/suggestions? The solution seems simple, just that I dont get
it yet.

Thanks!

解决方案

Pixel.to.life <pi***********@gmail.comwrote:

Here is a problem I am facing (it might be too simple, but then I
admit I am not a Guru:-)

I have a main thread, in managed C++, that deals with displaying a
form and some controls.

I invoke another thread for some processing from this thread, so
basically the main thread is waiting on the sub thread.

That''s your problem - don''t make the main thread wait. The point of
using other threads to do work in a UI application is to avoid the UI
locking up. You need to keep the UI event loop free to handle things
like screen refreshes.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com


On May 6, 1:21*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:

Pixel.to.life <pixel.to.l...@gmail.comwrote:

Here is a problem I am facing (it might be too simple, but then I
admit I am not a Guru:-)

I have a main thread, in managed C++, that deals with displaying a
form and some controls.

I invoke another thread for some processing from this thread, so
basically the main thread is waiting on the sub thread.


That''s your problem - don''t make the main thread wait. The point of
using other threads to do work in a UI application is to avoid the UI
locking up. You need to keep the UI event loop free to handle things
like screen refreshes.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

Thanks Jon.

I realised it after I posted.

Fow now I use WaitForSingleThread(..) API to wait on the sub thread.
Whats the alternative so the sub thread processing can be done
asynchronously?

I am looking at MsgWaitForMultipleObjects(..) instead to see if I can
get status right there.

Sorry I am a novice to multithreading with GUI.


On May 6, 1:21*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:

Pixel.to.life <pixel.to.l...@gmail.comwrote:

Here is a problem I am facing (it might be too simple, but then I
admit I am not a Guru:-)

I have a main thread, in managed C++, that deals with displaying a
form and some controls.

I invoke another thread for some processing from this thread, so
basically the main thread is waiting on the sub thread.


That''s your problem - don''t make the main thread wait. The point of
using other threads to do work in a UI application is to avoid the UI
locking up. You need to keep the UI event loop free to handle things
like screen refreshes.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

Also, I need to wait for the sub thread because the main thread has to
do some consilidation once the sub thread is done. If I remove the
WaitForSingleThread(...) call, how do I know the subthread is done?

Thanks.


这篇关于在主/子线程之间使用委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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