且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#睡眠为500毫秒

更新时间:2023-02-27 07:47:50

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.