WPF-如何使主UI线程等到渲染线程完成其工作为止 [英] WPF - How to make main UI thread wait until the render thread is done doing its stuff

查看:108
本文介绍了WPF-如何使主UI线程等到渲染线程完成其工作为止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个场景,我要启动一个子线程,然后再花一些时间在主UI线程中进行操作,然后将"busyIndi​​cator.IsBusy"设置为true.
当完成主UI线程中的执行后,我再次将IsBusy设置为false.
但是问题是子线程的委托中的代码直到主线程完成其工作使得使用BusyIndi​​cator毫无意义时才执行. 有什么方法可以让主线程等待子线程完成其任务.
这是我的代码示例:

Hi,

I have a scenario where I am starting a child thread which sets the ''busyIndicator.IsBusy'' as true before doing some time taking stuff in main UI thread.
and when the execution in main UI thread is done I am again setting IsBusy as false.
But the problem is that the code in the delegate of the child thread doesn''t get executed until the main thread is done doing its stuff making the use of the BusyIndicator pointless.
Is there any way to make the main thread wait until the child thread is done doing its task.
Here is my code sample:

Thread thread = new System.Threading.Thread(delegate()
            {
                
                testBusyIndicator.Dispatcher.Invoke(DispatcherPriority.Send,
                   (ThreadStart) delegate
                    {
                        testBusyIndicator.IsBusy = true;
                    });
                
            });
            thread.Start();
            
            //DoTimeTakingStuff()
            testBusyIndicator.IsBusy = false; // It can also be done in a separate thread but not needed I think.
            thread.Abort();



在此先感谢:)



Thanks in advance :)

推荐答案

首先,您永远都不应让主线程(我假设这是您的UI线程)等待任何事情.这不仅不好,而且也永远不需要.

您的代码示例根本没有任何意义,因为thread的方法实际上什么也没做.想想你在做什么.您正在尝试在UI线程和thread之间竞争testBusyIndicator的值之间创建竞争条件;分配将以某种随机顺序进行.我更喜欢关于竞赛条件的描述性术语,即对执行时间的不正确依赖".参见 http://en.wikipedia.org/wiki/Race_condition [ Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
First, you should never make main thread (which is your UI thread I assume) to wait for anything. Not only this is bad, this is also never needed.

You code sample makes no sense at all, because the method of your thread really does nothing. Think of what are you doing. You''re trying to create a race condition between you UI thread and thread competing over the value of testBusyIndicator; the assignment will be done in some random order. I prefer more descriptive term about race condition — "incorrect dependency on the time of execution". See http://en.wikipedia.org/wiki/Race_condition[^].

Now, I''m not answering what to do, because you don''t explaining what you want to achieve. The mistake of you question is trying to ask about something which does not have to have any purpose (and apparently does not have in this case); you should ask a question based on your ultimate purpose, not on your opinion on how it should be achieved, especially when you don''t explain what you want to achieve. I''m also not asking about what are you trying to do because it is apparent to me that you don''t understand it yourself.

Focus on your ultimate goal. Picture what useful work should your thread do, not changing the status of any indicator. Perhaps you should simply prevent UI from some action until your temporary thread''s work is complete. But this is very simple task: you should invoke change of UI status in the very beginning and the very end of the thread''s body, that is in the same thread, not too different threads. (By the way the call to Invoke makes sure that actual calls on UI controls are always done in the UI thread anyway.) Be aware of race condition and avoid it.

You will find detailed explanation of how invocation works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于WPF-如何使主UI线程等到渲染线程完成其工作为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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