Vb.Net 等待进程 30 秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6070187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:30:34  来源:igfitidea点击:

Vb.Net waiting for process 30secs

vb.net

提问by Suresh

I have three events to fire on button click.

我有三个事件要在按钮点击时触发。

after running the first event i want to wait for 30sec to wait for nex event to fire.

运行第一个事件后,我想等待 30 秒以等待 nex 事件触发。

how i can wait( i mean looping for 30 secs).

我怎么能等(我的意思是循环 30 秒)。

Thanks, Nag

谢谢,纳格

回答by Peyton Crow

You can try this to your code directly:

您可以直接在您的代码中尝试此操作:

    MessageBox.Show("Test") ' Execute your method 1
    System.Threading.Thread.Sleep(30000)
    MessageBox.Show("Test2") ' Proceed with the other one :)

回答by Richard

If you wait on the UI thread you'll block the whole UI and Windows will show your application as non-responding.

如果您在 UI 线程上等待,您将阻塞整个 UI,Windows 会将您的应用程序显示为无响应。

Better to:

最好:

  • Update the UI to show it is busy, including disabling controls to block user input.
  • Use a timer control (details depend on WinForms or WPF) to trigger an event after the time delay
  • Do the work in the timer's event handler.
  • 更新 UI 以显示它正忙,包括禁用阻止用户输入的控件。
  • 使用定时器控件(具体取决于 WinForms 或 WPF)在时间延迟后触发事件
  • 在计时器的事件处理程序中完成工作。

If the work is CPU or IO intensive (ie. likely to block for more than a few tens of milliseconds) then perform the work in the threadpool (eg. BackgroundWorkercomponent). Remember you'll need to use Control.Invoke to make any changes to the UI from the worker thread.

如果工作是 CPU 或 IO 密集型的(即可能阻塞超过几十毫秒),则在线程池(例如BackgroundWorker组件)中执行工作。请记住,您需要使用 Control.Invoke 从工作线程对 UI 进行任何更改。

回答by jams

You can use Thread System.Threading.Thread.Sleep(30000);to hold execution.

您可以使用 ThreadSystem.Threading.Thread.Sleep(30000);来保持执行。

回答by Nighil

use a timer for it and set an interval of 30 seconds to timer (1sec = 1000)

为它使用一个计时器并设置一个 30 秒的间隔来计时 (1sec = 1000)

timer1.Interval=30000