如何启动一个线程,以保持GUI刷新? [英] How to start a thread to keep GUI refreshed?

查看:136
本文介绍了如何启动一个线程,以保持GUI刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,触发冗长的处理窗口。我把处理在一个单独的线程,但是 - 让我吃惊 - 它使GUI反正冻结。无控制被刷新,我甚至不能移动窗口。

I have window with button which triggers lengthy processing. I put processing in a separate thread, but -- to my surprise -- it makes GUI frozen anyway. No control is refreshed, I cannot even move the window.

所以问题是如何启动线程,因此它不会干扰与图形用户界面,即让GUI将永远是最新的(在处理更改数据,GUI显示它的一些作品)?

So the question is how to start the thread, so it won't interfere with GUI, i.e. so the GUI would always be up to date (while processing I change the data, and GUI displays some pieces of it)?

这就是我如何开始螺纹currectly:

That is how I start thread currectly:

        var thread = new Thread(doLearn);
        thread.IsBackground = true;
        thread.Start();



编辑1



乔恩:

Edit 1

Jon:


  1. 我根本不

  2. 没有加入呼叫
  3. UI线程被单独留在家中 - 它只是坐在那里

的处理是一个大循环的数学运算,而不是即使分配内存,在UI身边,我有(WPF)绑定到数据,如主回路电流迭代次数控制。应当每次主循环滴答刷新。 。循环的计数器是触发与每一个变化(WPF经典绑定)OnPropertyChanged属性。

The processing is a big loop with math operations, not even allocating memory, on UI side I have controls with binding (WPF) to data, like the number of current iteration of the main loop. It should be refreshed each time the main loop "ticks". The counter of the loop is a property which triggers OnPropertyChanged with each change (classic WPF binding).

好了,乔恩撞钉在头(谁是惊喜;-D?) - 谢谢!这个问题源于改变计数器。当我用,而不是柜台,本地计数器的图形用户界面进行了改版 - 我的意思是,我可以移动窗口,但是...我无法看到柜台的陈列

Ok, so Jon hit the nail at the head (who is surprises? ;-D) -- thank you! The problem comes from changing the Counter. When I used instead the Counter, local counter the GUI was refreshed -- I mean I could move windows, but... I couldn't see display of the Counter.

我有什么在这里 - 一个WPF图形用户界面,这样的数据绑定

What I have here -- a WPF GUI, with such data-binding

<TextBlock Text="{Binding Path=Counter"/>

和我当然有反财产上的每个变化发送事件的PropertyChanged。一位听众是肯定的图形用户界面。

and I have Counter property of course which on each change sends event PropertyChanged. One of the listeners is for sure GUI.

所以,乔恩的答案是有效的答案,但是从好的设计POV不完全的,因为如果GUI部分有拉约占计数器的信息,并更新显示每隔(比方说)3秒,为什么会有人使用数据绑定?对我来说,这种方法无效数据绑定的想法。

So, Jon answer is valid "the answer", but from good design POV not exactly, because if GUI part has to pull up the info about Counter and update the display every (let's say) 3 seconds, why would anyone use data binding? For me such approach invalidates data binding idea.

我可以,理论上,传递给处理线程的GUI调度,并做好GUI线程所有的发送,故能工作(我没有尝试),但将意味着非GUI部分和GUI部分的紧密耦合。

I could, theoretically, pass to the processing thread the GUI dispatcher, and do all the sending in GUI thread, and it could work (I didn't try it) but it would mean tight coupling of non-GUI part and GUI part.

到目前为止,我不知道该怎么做正确的方法。最合理的猜测,到目前为止是创造TimerDispatcher但不是在GUI的一面,但处理库中,并立即更新计数器的值,但做不时所有的发送(我没有尝试,尚未虽然)。

So far, I have no idea how to do it "right" way. The best guess so far is to create TimerDispatcher but not at GUI side but inside the processing library, and update Counter value immediately but do all the sending from time to time (I didn't try it yet though).

小的话:我有更多的属性实际上绑定,就像IsRunning,这是在开始和处理的最后改变。而这些变化是否正确地影响显示器 - 但柜台变化触发3-4秒3000左右通知。所以它看起来像干扰问题。我做了另外一个测试 - 我杀了部分数据绑定,所以发出了通知,但GUI不接受他们 - 但听他们说。在这种情况下,图形用户界面也被冻结

所以,我还是听着都建议 - 谢谢你提前分享

So, I am still listening to all advices -- thank you advance for sharing.

的传奇继续在这里:

如何做加工,并保持GUI使用数据绑定刷新<? / A>

How to do the processing and keep GUI refreshed using databinding?

推荐答案

这应该是罚款,因为它是。事情可能会冻结你的用户界面:

It should be fine as it is. Things which may be freezing your UI:


  • 您是否在UI线程内锁,并在其他线程锁定在同一个锁? / LI>
  • 你叫加入从UI线程的线程上?

  • 你做一些的其他的在UI线程繁重的工作?

  • Are you locking within the UI thread, and locking on the same lock in your other thread?
  • Are you calling Join on the thread from your UI thread?
  • Are you doing some other heavy work in the UI thread?

如果您能想出一个简短而完整的程序,说明这个问题,我敢肯定,我们可以帮助解决它......但可以肯定的的好起来。

If you could come up with a short but complete program which shows the problem, I'm sure we could help to fix it... but it certainly should be okay.

编辑:好吧,现在你已经添加了这一点:

Okay, now you've added this:

循环的计数器触发每次更改OnPropertyChanged属性(WPF经典绑定)

The counter of the loop is a property which triggers OnPropertyChanged with each change (classic WPF binding).

所以,你要更新来自非UI线程的财产? 。我预计会导致问题,因为它会触发错误的线程用户界面的变化

So you're updating the property from the non-UI thread? I would expect that to cause problems, because it will trigger UI changes from the wrong thread.

我建议你采取的方法,如:

I suggest you take an approach such as:


  • 更新定期通过 Dispatcher.BeginInvoke

  • 柜台都有 UI计数器和工人柜台 - 从复制值通过 DispatcherTimer ,基本上是轮询在UI线程工人柜台到UI计数器它

  • Periodically update the counter via Dispatcher.BeginInvoke
  • Have the "UI counter" and the "worker counter" - and copy the value from the "worker counter" to the "UI counter" in the UI thread via a DispatcherTimer, essentially polling it.

这篇关于如何启动一个线程,以保持GUI刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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