Are comments used regularly in programming

Create a regular work item

  • 2 minutes to read

Important APIs

Here's how to create a work item that will repeat regularly.

Create the regular work item

Use the CreatePeriodicTimer-Method to create a regular work item. Provide a Lambda function to do the work and use the periodParameter specifies the interval between transmissions. The interval is determined using a TimeSpan-Structure specified. The work item is sent again each time the interval has elapsed. So make sure it is long enough to do the job.

CreateTimer gives a ThreadPoolTimerObject back. Save the item in case the timer needs to be canceled.

Note Avoid a value of 0 (or less than a millisecond) for the interval. Otherwise, the regular timer will act like a one-time timer.

Note You can " coredispatcher. runasync Use "to access the user interface and view the progress of the work item.

The following example creates a work item that runs every 60 seconds:

Handle the cancellation of the regular work item (optional)

If necessary, you can cancel the regular timer with a TimerDestroyedHandlerProcess element. Use the CreatePeriodicTimer-Overload an additional Lambda function ready, which handles the cancellation of the regular work item.

The following example creates a recurring work item that repeats every 60 seconds and also provides a cancellation handler:

Cancel the timer

If necessary, call the CancelMethod to stop repeating the regular work item. If the work item is running when the recurring timer is canceled, it can still be completed. The TimerDestroyedHandlerElement (if used) is called when all instances of the regular work item have been completed.

Remarks

For information about one-time timers, see Submit a work item using a timer.

Related topics