简体   繁体   中英

Automatically call a task every 5 seconds

i have an Infragistics carousel control which i want to call "next" on every 5 seconds. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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