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

问题描述

亲爱的,


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

承认我不是大师: - )


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

表格和一些控件。


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

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


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

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


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

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

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


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

它。


谢谢!

解决方案



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

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

就像屏幕刷新一样。


-

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

网站:

博客:

C#深度:




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

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

就像屏幕刷新一样。


-

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

网站:

博客:

C#深度:



谢谢Jon。


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


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

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


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


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



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

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

就像屏幕刷新一样。


-

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

网站:

博客:

C#深度:



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

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

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!

解决方案

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



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 - <[email protected]>
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.



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 - <[email protected]>
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.


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

07-25 13:55