简体   繁体   English

每5秒自动调用一次任务

[英]Automatically call a task every 5 seconds

i have an Infragistics carousel control which i want to call "next" on every 5 seconds. 我有一个Infragistics轮播控件,我想每5秒调用一次“下一个”。 However im not sure how to do this without a while true. 但是我不确定如何做到这一点,没有一会儿真的。

Any help would be great, thanks! 任何帮助都会很棒,谢谢!

Currently its kicked off from a click, starting a task within a while true. 目前,它从点击开始,在一段时间内启动任务。 Obviously this i what i want to avoid. 这显然是我想要避免的。

 private void GoToNext(object sender, MouseButtonEventArgs e)
    {

        while (true)
        {


            Task task = new Task(() => MyCarousel.Dispatcher.BeginInvoke(new next(goToNext), null));
            task.Start();

        }

    }

private bool goToNext()
    {
        Thread.Sleep(15);
        MyCarousel.ExecuteCommand(Infragistics.Windows.Controls.XamCarouselPanelCommands.NavigateToNextItem);
        return true;
    }

Timers are used for this in general, sitting in the background and triggering every so often. 定时器通常用于此,坐在后台并经常触发。 There are a number in .NET (in System.Timers and elsewhere) and which one is best depends on your particular scenario. .NET中有一个数字(在System.Timers和其他地方),哪一个最好取决于您的特定场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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