[英]How do I trigger a method to run after x seconds?
I am working on a C# Windows Forms application where I need the method to pause for 30 seconds until it continues on to the next line of code.我正在开发一个 C# Windows Forms 应用程序,我需要该方法暂停 30 秒,直到它继续到下一行代码。 I have tried Thread.Sleep()
which wasn't suitable for this application and I've understood that I should use some sort of timer instead.我已经尝试过不适合这个应用程序的Thread.Sleep()
并且我明白我应该使用某种计时器。 I have searched a lot but I can't figure out how to implement timers.我搜索了很多,但我不知道如何实现计时器。
My code is below and could anyone show me how I should implement the timer.我的代码在下面,谁能告诉我应该如何实现计时器。 I have made a comment on where I want the method to pause.我已经对我希望该方法暂停的位置发表了评论。
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();
}
If your app is a .Net 4.5 application, then it's somewhat easier to use Task
: 如果您的应用程序是.Net 4.5应用程序,那么使用Task
更容易一些:
private async void start_Vid_Click(object sender, EventArgs e)
{
if (video.State != StateFlags.Running)
{
viewport.Visible = true;
video.Play();
}
await Task.Delay(TimeSpan.FromSeconds(30));
viewport.Visible = false;
viewport2.Visible = true;
pictureBox1.Visible = true;
start_Webc();
video2.Play();
}
Task.Delay is not available on .NET 4.0, but you can start task, which will just sleep for 30 seconds, and later you can continue on UI thread again: Task.Delay在.NET 4.0上不可用,但您可以启动任务,它将只睡眠30秒,之后您可以再次继续使用UI线程:
Task.Factory.StartNew(() => Thread.Sleep(30 * 1000))
.ContinueWith((t) =>
{
viewport.Visible = false;
viewport2.Visible = true;
pictureBox1.Visible = true;
start_Webc();
video2.Play();
}, TaskScheduler.FromCurrentSynchronizationContext());
System.Threading.Tasks.Task.Delay( 60 * 1000 ).ContinueWith( (_) => StartVoid() );
At class level define an instance of Timer
(There are a few classes like this - for a winforms app you should use System.Windows.Forms.Timer
) 在类级别定义一个Timer
实例(有这样的几个类 - 对于winforms应用程序,你应该使用System.Windows.Forms.Timer
)
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
Then in your method add lines to handle its tick and start it: 然后在你的方法中添加行来处理它的tick并启动它:
private void start_Vid_Click(object sender, EventArgs e)
{
if (video.State != StateFlags.Running)
{
viewport.Visible = true;
video.Play();
}
myTimer.Tick += (o,ea) => {
viewport.Visible = false;
viewport2.Visible = true;
pictureBox1.Visible = true;
start_Webc();
video2.Play();
myTimer.Stop();
}
myTimer.Interval = 5000; // 5 seconds
myTimer.Start();
}
反应性扩展也有一种方法:
Observable.Return("Delayed").Delay(TimeSpan.FromSeconds(30)).Subscribe(val => Console.WriteLine("Executing delayed"));
We can do that using Task from 我们可以使用Task from来做到这一点
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
Thread.Sleep(5000);
this.Invoke(new Action(() =>
Method1()));
});
case WPF..案例 WPF..
public static partial class WpfUtils
{
public static void RunAfter(double seconds, Action action)
{
if (Application.Current.Dispatcher == null)
return;
var myTimer = new DispatcherTimer(
DispatcherPriority.Normal,
Application.Current.Dispatcher)
{
Interval = TimeSpan.FromSeconds(seconds)
};
myTimer.Tick += (_, _) =>
{
action?.Invoke();
myTimer.Stop();
};
myTimer.Start();
}
}
WpfUtils.RunAfter(3, () =>
{
// some code
Console.WriteLine("Running after 3 seconds");
});
Note that this code will run on the UI thread.. on the other hand, if you go with methods from Task
class, the delayed code will run on a background thread.请注意,此代码将在 UI 线程上运行。另一方面,如果您使用来自Task
class 的方法的 go,则延迟代码将在后台线程上运行。
Task.Delay(6 * 1000).ContinueWith(_ =>
{
// this will run in a background-thread
var b = Thread.CurrentThread.IsBackground;
Debug.Assert(b);
Console.WriteLine("3");
});
var timer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(3)
};
timer.Tick += (_, _) =>
{
// this will run in the UI-thread
var b = Thread.CurrentThread.IsBackground;
Debug.Assert(!b);
Console.WriteLine("2");
timer.Stop();
};
timer.Start();
Console.WriteLine("1");
// output
// 1
// 2
// 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.