繁体   English   中英

如何触发方法在 x 秒后运行?

[英]How do I trigger a method to run after x seconds?

我正在开发一个 C# Windows Forms 应用程序,我需要该方法暂停 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();
}

如果您的应用程序是.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在.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() );

在类级别定义一个Timer实例(有这样的几个类 - 对于winforms应用程序,你应该使用System.Windows.Forms.Timer

static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();

然后在你的方法中添加行来处理它的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"));

我们可以使用Task from来做到这一点

System.Threading.Tasks.Task.Factory.StartNew(() =>
        {
            Thread.Sleep(5000);
            this.Invoke(new Action(() => 
                Method1()));
        });

案例 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");
});

请注意,此代码将在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM