Time execution in time

Asked

Viewed 277 times

0

As I do in the universal app win 10 to perform a code execution of 10 in 10 sec. In the form would use adding a team. Please help me.

  • Make a while true with something like the Sleep thread.

  • How do I use the Sleep thread?

  • System.Threading.Tasks.Task.Delay(3000). Wait() http://stackoverflow.com/questions/12641223/thread-sleep-replacement-in-net-for-windows-store

2 answers

0

There are some possibilities:

ThreadPoolTimer:

public MainPage()
{
    InitializeComponent();
    ThreadPoolTimer timer = ThreadPoolTimer.CreatePeriodicTimer(async (t) =>
    {
        // do some work not connected with UI
        Debug.WriteLine($"executado: {DateTime.Now}");
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            // do some work on UI here;
            //MyTextBlock.Text = "Test";
        });
    }, TimeSpan.FromMinutes(1));
}

Another possibility DispatcherTimer (this has the advantage of updating UI directly in the delegated method)

private readonly DispatcherTimer _mytimer = new DispatcherTimer();
public MainPage()
{
    InitializeComponent();
    _mytimer.Interval = new TimeSpan(0, 0, 0, 5, 0);
    _mytimer.Tick += Mytimer_Tick;
    _mytimer.Start();
}

private void Mytimer_Tick(object sender, object e)
{
    Debug.WriteLine($"executado: {DateTime.Now}");
    // do some work on UI here;
    //MyTextBlock.Text = "Test";
}

Another possibility is to use a Trigger (trigger) run in the background of minutes in minutes.

Yes, minutes, one Trigger of the kind Timer can only be defined from 15 minutos (if you try to set a value less than 15 error will occur), that is, every 15 minutes your Trigger/Timer is fired. The initial time is not based on the moment when the Trigger and yes every 15 minutes "full", for example 00:15, 00:30, ...

public sealed partial class MainPage
{
    public MainPage()
    {
        InitializeComponent();
        RegisterTask();
    }

    private void RegisterTask()
    {
        //create a task/timer to run each 15 minutes (minimum allowed 15min)
        var builder = new BackgroundTaskBuilder
        {
            Name = "My Background TimerTrigger"
        };
        builder.SetTrigger(new TimeTrigger(15, false));
        builder.Register();
        Debug.WriteLine($"RegisterTask {DateTime.Now}");
    }
}

sealed partial class App 
{
    protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
    {
        //Can be fired more than once per trigger, check it out.
        Debug.WriteLine($"OnBackgroundActivated - {DateTime.Now}");
    }
}

Source Code:
https://github.com/rubgithub/Timer-UWP

References:
https://docs.microsoft.com/pt-br/windows/uwp/launch-resume/run-a-background-task-on-a-timer- https://docs.microsoft.com/en-us/uwp/api/Windows.ApplicationModel.Background.TimeTrigger https://github.com/Microsoft/Windows-universal-samples/blob/6370138b150ca8a34ff86de376ab6408c5587f5d/Samples/BackgroundActivation/cs/SampleConfiguration.cs https://stackoverflow.com/questions/34271100/timer-in-uwp-app-which-isnt-linked-to-the-ui

0


  • But where does the code enter that will be executed in time?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.