mirror of
https://github.com/BililiveRecorder/BililiveRecorder.git
synced 2024-11-16 03:32:20 +08:00
43 lines
1.1 KiB
C#
43 lines
1.1 KiB
C#
/**
|
|
* Author: Roger Lipscombe
|
|
* Source: https://stackoverflow.com/a/7472334
|
|
* */
|
|
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BililiveRecorder.Core
|
|
{
|
|
public static class Repeat
|
|
{
|
|
public static Task Interval(
|
|
TimeSpan pollInterval,
|
|
Action action,
|
|
CancellationToken token)
|
|
{
|
|
// We don't use Observable.Interval:
|
|
// If we block, the values start bunching up behind each other.
|
|
return Task.Factory.StartNew(
|
|
() =>
|
|
{
|
|
for (; ; )
|
|
{
|
|
action();
|
|
if (token.WaitCancellationRequested(pollInterval))
|
|
break;
|
|
}
|
|
}, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
|
|
}
|
|
}
|
|
|
|
internal static class CancellationTokenExtensions
|
|
{
|
|
public static bool WaitCancellationRequested(
|
|
this CancellationToken token,
|
|
TimeSpan timeout)
|
|
{
|
|
return token.WaitHandle.WaitOne(timeout);
|
|
}
|
|
}
|
|
}
|