C# 睡眠 500 毫秒

发布于 2024-08-27 00:37:09 字数 380 浏览 3 评论 0原文

您能告诉我如何将程序暂停 500 毫秒然后继续吗?

我读到 Thread.Sleep(500) 不好,因为它会占用 GUI 线程。

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

我只想等待 500 毫秒,然后继续执行下一条语句。

请指教。

编辑:我需要显示状态栏消息 500 毫秒,然后用不同的消息更新该消息。抱歉,我的意思是 500 而不是 50。

编辑:我明白你所说的一切。但是:[我只想等待 500 毫秒,然后继续执行下一条语句。] 我想因为时间间隔很短,所以我将在主 GUI 线程上执行 Thread.Sleep(500)。否则我将不得不重写大量代码来适应这 500 毫秒的短暂间隔。

编辑:我将尝试重新格式化我的状态消息,这样就不需要暂停。

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

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

Using a timer it fires a callback ...

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

Please advise.

EDIT: 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.

EDIT: 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

池木 2024-09-03 00:37:09

Hmya,您想要做的事情从根本上来说与 Windows 编程模型不兼容。本机 Windows 程序是事件驱动的。你的程序总是空闲的,坐在由Application.Run()启动的循环中,等待Windows告诉它发生了一些有趣的事情,它应该做出响应。绘画请求、鼠标点击、计时器到期等等。

您的程序应该对此做出响应并过滤您感兴趣的内容。当您将按钮放在窗体上时,您总是对 Windows 发送 MouseDown 通知消息时生成的 Click 事件感兴趣。您的 Click 事件处理程序运行您编写的某种自定义代码。就像在您的情况下更新状态栏消息一样。

半秒后更新状态栏消息没有任何意义。在那 500 毫秒内究竟发生了什么改变了程序响应事件的方式?您可以调用 StatusBar 的 Update() 方法,以便新消息可见,然后调用 System.Threading.Thread.Sleep(500) 来获取您想要的内容。你会侥幸逃脱惩罚的,Windows 发出的“无响应”幽灵会让你的程序死机几秒钟。

但这没有多大意义,在那半秒内什么也没有发生,程序的状态没有改变。它无法改变,它对 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.

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.

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.

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.

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.

喵星人汪星人 2024-09-03 00:37:09

您有两种选择:

  • 按照您的建议使用计时器。将您的方法分为两个方法:foo1 和 foo2。使用 foo1 启动计时器并在回调中运行 foo2。
  • 使用 BackgroundWorker 运行整个函数并使用Thread.Sleep 在工作线程上。

从您的更新看来,您唯一想做的就是更改单个字段。我肯定会推荐第一种方法:使用计时器。为这个任务启动一个BackgroundWorker是多余的,只会给你带来不必要的额外工作和复杂性。

You have two choices:

  • Use a timer as you suggested. Split your method up into two methods, foo1 and foo2. Use the foo1 to start the timer and run foo2 in the callback.
  • Use a BackgroundWorker for running the entire function and use Thread.Sleep on the worker thread.

From your update it seems that the only thing you want to do is change a single field. I would definitely recommend the first method: using a timer. Starting a BackgroundWorker for this task is overkill and will just give you unnecessary extra work and complications.

浅浅 2024-09-03 00:37:09

您始终可以使用 后台工作者。这将导致您的回调在单独的线程中运行,您可以在其中使用 Thread.Sleep 来暂停它,而不会阻塞 UI。然后,完成后,只需用新消息更新状态栏即可。

Instead of pausing the UI directly for 500 ms, you can always use a BackgroundWorker. That will cause your callback to run in a separate thread, where you can use Thread.Sleep to pause it without blocking the UI. Then when you are done, just update the status bar with your new message.

青柠芒果 2024-09-03 00:37:09

该问题的更多背景信息会有所帮助。

Thread.Sleep(50) 会将当前线程暂停 50 毫秒。如果您在 UI 线程中执行此操作,那么是的,它会将 UI 冻结 50 毫秒。但是,如果您使用不同的线程来执行此处理,则在该线程上调用 Sleep 会将其暂停 50 毫秒,而不会冻结您的 UI 线程。

请参阅 Marc 对问题的回答,了解使用 BackgroundWorker 实例来执行您需要的操作。

More context to the question would be helpful.

Thread.Sleep(50) will pause the current thread for 50 milliseconds. If you're doing this in the UI thread, then yes, it will freeze the UI for 50 milliseconds. However, if you use a different thread to do this processing, then calling Sleep on that thread will pause it for 50 milliseconds without freezing your UI thread.

See Marc's answer to this question for an example on using a BackgroundWorker instance to do what you need.

橪书 2024-09-03 00:37:09

在 C# 中,最好的选择是使用计时器并触发回调。

在 F# 中,有一种很棒的方法可以完成您想要的操作,请参阅

F# 异步在客户端

它展示了如何编写直线代码并让语言为您处理回调。

In C# your best bet is to use the Timer and fire a callback.

In F# there is an awesome way to do what you want, see

F# async on the client side

which shows how to write straight-line code and have the language take care of the callbacks for you.

初见终念 2024-09-03 00:37:09

您需要分配另一个线程。在该线程中,您Sleep(500) 并更改所需的数据。注意:您需要使用原始线程的调度程序,因为与 UI 相关的数据通常应该从 GUI 线程更新。

You need to allocate another thread. In that thread you Sleep(500) and change the needed data. Caution: you would need to use the original thread's dispatcher, because the data related to UI should be usually updated from the GUI thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文