C# - 替代视频下载? [英] C# - Alternative to Thread.Sleep?

查看:175
本文介绍了C# - 替代视频下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的这一切在C#,Visual Studio 2008中。

I'm doing all this in C#, in Visual Studio 2008.

我想慢下来我的算法的工作,使用户可以看它的工作。有一个在GUI中不可见,所以我每一个实例后添加的Thread.Sleep周期性变动。

I want to slow down the work of my algorithm so that the user can watch it's work. There is a periodic change visible at the GUI so I added "thread.sleep" after every instance.

问题是,Thread.sleep代码,当设置为至少做第二,Thread.sleep代码(几圈之后)的少数情况后,只是冻结整个图形用户界面,并保持这种方式,直到完成计划。不是马上,但它总是happends。多久取决于睡眠的长度。

Problem is that "thread.sleep", when set to atleast a second, after a few instances of "thread.sleep" (after few loops) simply freezes entire GUI and keeps it that way till program completion. Not right away, but it always happends. How soon depends on the length of the sleep.

我有证据证明整个程序不冻结,它的工作是一回事,甚至睡眠正在正确长度的暂停。但是GUI冻结在某一点,直到算法结束,此时它显示了正确的最终状态。

I have proof that entire program does not freeze, it's working it's thing, even the sleep is making pauses of correct length. But the GUI freezes at certain point until the algorithm ends, at which point it shows the correct final state.

如何解决这个问题?替代在某一点暂停算法?

How to solve this issue? Alternative to pausing algorithm at certain point?

推荐答案

首先,不要让用户等待是他们以前所做的工作别想什么时候会完成。它没有意义的。请,只是说没有。

First off, don't make the user wait for work that is done before they even think about when it will be finished. Its pointless. Please, just say no.

二,你在沉睡在UI线程。这就是为什么UI线程被锁定。在UI线程不能堵塞;如果是这样,UI线程无法在窗体上更新控制和系统响应消息。响应系统消息是UI线程的一项重要任务;不这样做,使您的应用程序出现锁定系统。不是一件好事。

Second, you're "sleeping" the UI thread. That's why the UI thread is "locking up." The UI thread cannot be blocked; if it is, the UI thread cannot update controls on your forms and respond to system messages. Responding to system messages is an important task of the UI thread; failing to do so makes your application appear locked up to the System. Not a good thing.

如果你想做到这一点(请不要)只需创建一个的定时器当你开始做的工作,当它的,表明它的时间停止假装做的工作。

If you want to accomplish this (please don't) just create a Timer when you start doing work that, when it Ticks, indicates its time to stop pretending to do work.

再次请不要'T做到这一点。

Again, please don't do this.

这篇关于C# - 替代视频下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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