Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void StartBackgroundTasks(CancellationTokenSource cancelTokenSrc)
- {
- void TaskAction()
- {
- DoWork();
- }
- void HeartbeatAction()
- {
- var task = TaskFactory.CreateAndStartLongRunningTask(
- TaskAction, cancelTokenSrc.Token);
- while (true)
- {
- // checks Task.Status to figure out when to stop
- if (!TryPerformHeartbeat(task)) break;
- Task.Delay(TimeSpan.FromSeconds(20)).Wait();
- }
- }
- TaskFactory.CreateAndStartLongRunningTask(HeartbeatAction);
- }
- internal static class TaskFactory
- {
- internal static Task CreateAndStartLongRunningTask(
- Action action, CancellationToken? token = null)
- {
- return Task.Factory.StartNew(
- action,
- token ?? CancellationToken.None,
- TaskCreationOptions.LongRunning,
- TaskScheduler.Default);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement