C#睡眠为500毫秒 [英] C# Sleep for 500 milliseconds

查看:1283
本文介绍了C#睡眠为500毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能告诉我怎么去有关暂停我的计划,500毫秒,然后再继续?

Could you please tell me how do I go about pausing my program for 500 milliseconds and then continue?

我读 Thread.sleep代码( 500)是不好的,因为它包含了GUI线程。

I read Thread.Sleep(500) is not good as it holds up the GUI thread.

使用它触发回调的计时器...

Using a timer it fires a callback ...

我只是想等待500毫秒,然后继续下一个语句。

I just want to wait 500ms and then continue to the next statement.

请指教。

编辑:我需要显示状态栏信息为500毫秒,然后用一个不同的更新消息。对不起,我的意思不是500 50

I need to display a status bar message for 500ms and then update the message with a different one. Sorry, I meant 500 not 50.

编辑:我明白你说的一切。但是:我只是想等待500毫秒,然后继续下一个声明。]我想是因为它是这样一个短的时间间隔我要去做到主界面线程上的Thread.Sleep(500)。否则我将不得不重新编写大量的代码,以容纳500毫秒这短暂的时间

I do understand what all you have said. but: [I just want to wait 500ms and then continue to the next statement.] I think because it is such a short interval i am going do a Thread.Sleep(500) on the main GUI thread. Otherwise i would have to rewrite a lot of code to accomodate this brief interval of 500 milliseconds.

编辑:我会尝试重新格式化我的状态消息,因此不需要暂停

i will try to reformat my status message so the pause is not needed.

推荐答案

Hmya,你想要做的是与Windows编程模型非常根本不相容的。原生的Windows程序是事件驱动的。你的程序是总是空的,坐姿)通过Application.Run(开始一个循环中,等待Windows来告诉它一些有趣的事情发生了,它应该响应。涂料的要求,鼠标点击,计时器到期,这样的东西。

Hmya, what you're trying to do is pretty fundamentally incompatible with the Windows programming model. A native Windows program is event driven. Your program is always idle, sitting inside a loop started by Application.Run(), waiting for Windows to tell it that something interesting happened that it should respond to. Paint requests, mouse clicks, timer expirations, stuff like that.

您的程序应该对此作出回应和过滤有意思的东西给你。当你把一个表单上的按钮,你的总是的感兴趣的Click事件,当Windows发送的MouseDown通知消息产生。点击事件处理程序运行某种类型的自定义代码,你写。像你的情况更新状态栏消息。

Your program should respond to this and filter what is interesting to you. When you drop a button on a form, you are always interested in the Click event, generated when Windows sends the MouseDown notification message. Your Click event handler runs some kind of custom code that you write. Like updating a status bar message in your case.

更新状态栏消息半秒后没有弄懂整个heckofalot。在这些500毫秒,改变你的程序响应事件的方式究竟发生了什么?您可以调用状态栏的更新()方法,因此新的消息是可见的,则调用System.Threading.Thread.Sleep(500),以得到你想要的。你会逃脱它的不响应鬼了Windows搭设需要你的程序几秒钟会死。

Updating the status bar message half a second later doesn't make a whole heckofalot of sense. What exactly happened during those 500 milliseconds that changed the way your program responds to events? You can call the Update() method of the StatusBar so the new message is visible, then call System.Threading.Thread.Sleep(500) to get what you want. You'll get away with it, the "Not Responding" ghost that Windows puts up takes your program going dead for several seconds.

但是,这并不会让很多感,没有什么半秒期间发生的事情,你的程序的状态并没有改变。它不能改变,那是死的Windows,而不是接受,将允许它改变状态的任何消息。

But that doesn't make a lot of sense, nothing happened during that half second, the state of your program didn't change. It couldn't change, it was dead to Windows and not receiving any messages that would allow it to change state.

嗯,这是关于据我可以利用这个。请更新您的问题,并解释为什么你需要这样做。以防万一:如果你正在考虑这个假做一些半秒钟重要的是,你的用户将不会留下深刻的印象。她最终会发现你的UI是死了没有什么可以炫耀它半秒。

Well, that's about as far as I can take this. Please update your question and explain why you need to do this. Just in case: if you're contemplating this to fake doing something important for half a second, your user will not be impressed. She'll eventually notice your UI is dead for half a second without anything to show for it.

这篇关于C#睡眠为500毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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