【发布时间】:2019-09-19 04:03:07
【问题描述】:
我正在开发一个 C# Windows 窗体应用程序,我需要该方法暂停 30 秒,直到它继续执行下一行代码。我试过Thread.Sleep(),它不适合这个应用程序,我知道我应该使用某种计时器。我已经搜索了很多,但我无法弄清楚如何实现计时器。
我的代码如下,谁能告诉我应该如何实现计时器。我已经对我希望方法暂停的位置发表了评论。
private void start_Vid_Click(object sender, EventArgs e)
{
if (video.State != StateFlags.Running)
{
viewport.Visible = true;
video.Play();
}
//Here I want to wait 30 seconds until the next line of code is triggered
viewport.Visible = false;
viewport2.Visible = true;
pictureBox1.Visible = true;
start_Webc();
video2.Play();
}
【问题讨论】:
-
创建一个
System.Windows.Forms.Timer。 -
为什么 Thread.Sleep 不适合这个应用?
-
@SergeyS Thread.Sleep 很少合适,除非您明确地在自己的线程上。在这种情况下,它将完全阻塞 UI,使界面无用并中断视频。