22/02/02 11:00:41.80 tkuApL0q0.net
CancellationToken使おう
private CancellationTokenSource cts = new CancellationTokenSource();
private Task task;
public void Start()
{
this.cts = new CancellationTokenSource();
this.task = Test( cts.Token );
}
public void Stop()
{
this.cts.Cancel();
this.task.Wait(); // 必要があれば
}
static async Task Test( CancellationToken ct )
{
while ( ! ct.IsCancellationRequested )
{
SendKeys.Send( "Break" );
try
{
await Task.Delay( 10000, ct );
}
catch ( TaskCanceledException )
{
}
}
}