形成不响应时,在C#中进行任何其他操作 [英] Form Not Responding when any other operation performed in C#

查看:95
本文介绍了形成不响应时,在C#中进行任何其他操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格以进度条(使用VS2010 C#开发)。
这是一种秒表形式,其中我填的是进度条中说10secs的......随着时间的推移,进度栏填写相应的手段....之后5secs,进度条将充满50%等....

I have a form (Developed in C# using VS2010) with a Progress Bar. It's kind of stopwatch form where I fill the progress bar in say 10secs.... As Time elapses, Progress bar fills accordingly.... Means after 5secs, Progress Bar will be filled 50% and so on....

我使用了一个for循环来执行此操作: -

I used a for loop to perform this operation:-

for(int count=0;count<200;count++)
{
   progbar.performstep();
   progbar.update();
   update();
   Thread.Sleep(50);
}

我用50毫秒的Thread.sleep代码,使进度条顺利更新。
对于1秒,它将使步骤。

I have used Thread.Sleep of 50msec so that progress bar updates smoothly. For 1sec, it increments in steps.

问题是,如果我不喜欢运动这种形式,甚至点击桌面上的另一个图标电气特性的操作使得这种形式的没有响应......但它perfoms的行动,在10秒结束时,它重新获得它的状态并显示指示10secs弹​​出的经过与进度条满。

Problem is if I do anyother operation like Moving this form or even clicking on another icon on desktops makes this form "Not Responding".... But it perfoms the operation and at the end of 10 secs, It regains it's state and shows the pop up indicating 10secs are elapsed with Progress Bar Full.

感谢您的帮助和抱歉使用这种复杂的语言。

Thanks for help and Sorry for using such complicated language.

问候,
Swanand

Regards, Swanand

更新:我解决了这个问题,下面的答案的帮助....一个常见的​​错误我才知道是忘记 Applications.DoEvents()伴随着更新()......如果你进入这一行,也有越来越少的机会吊死!

Update: I solved this problem with the help of Answers below.... One common mistake I got to know is forgetting "Applications.DoEvents()" along with "update()".... If you enter this line, there are less chances of getting "hanged"!

推荐答案

您所执行的UI线程上长时间运行的操作,这意味着UI消息循环(负责处理事件,例如用户输入,更新屏幕)没有得到机会运行。

You're performing a long-running operation on the UI thread, which means that the UI "message loop" (responsible for handling events such as user input and updating the screen) doesn't get a chance to run.

您应该在不同的线程中执行长时间运行操作(一个人是否自己创建或后台线程),要么使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx\"><$c$c>BackgroundWorker轻松更新您的进度栏,或使用 Control.Invoke /的BeginInvoke 马歇尔委托回调到UI线程,当你需要更新的用户界面。 (你不能从错误的线程更新控制。)

You should perform the long-running operation on a different thread (whether one you create yourself or a background thread) and either use BackgroundWorker to easily update your progress bar, or use Control.Invoke/BeginInvoke to marshall a delegate call back to the UI thread when you need to update the UI. (You mustn't update controls from the wrong thread.)

如果你唯一的UI交互操作是在进度条填充,我建议使用的BackgroundWorker

If your only UI interaction is filling in a progress bar, I suggest using BackgroundWorker.

如果你没有真正做实的工作,只等时间过去,你可以使用 System.Windows.Forms.Timer 而不是全部此,但是。在UI线程上,将嘀,但不会阻止刻度之间的UI线程。你应该只使用这个,如果你没有很多工作做虽然 - 如果它的真正的仅仅是更新进度条,而不是(说)处理文件等等。注意,你不应该 ŧ依靠计时器准时正是射击 - 你应该设置进度条的基础上,观察时的位置,而不是蜱的数量观察

If you're not really doing "real" work, just waiting for time to pass, you could use a System.Windows.Forms.Timer instead of all of this, however. That will "tick" on the UI thread, but won't block the UI thread between ticks. You should only use this if you don't have a lot of work to do though - if it really is just updating a progress bar, not (say) processing a file etc. Note that you shouldn't rely on the timer firing exactly "on time" - you should probably set the position of the progress bar based on the observed time, rather than the observed number of ticks.

这篇关于形成不响应时,在C#中进行任何其他操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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