private static async Task RunSampleAsync(CleanupGuard guard, TraceSource traceSource, CancellationToken token) { // Make sure we switch to another thread before starting the work. await Task.Yield(); string timestamp = DateTime.Now.ToString("yyyyMMhhmmss"); string filePrefix = Environment.ExpandEnvironmentVariables(@"%TEMP%\CleanupSample." + timestamp + "."); Random random = new Random(); char[] alphabet = new char[] { 'z', 'x', 'c', 'v', 'b', 'n', 'm' }; int index = 0; while (!token.IsCancellationRequested) { string fileName = filePrefix + index + ".txt"; guard.Register(() => ZeroFileAsync(fileName, traceSource)); traceSource.TraceInformation("Writing file '{0}'...", fileName); await WriteFileAsync(fileName, random, alphabet, token); traceSource.TraceInformation("Starting 'notepad.exe {0}'...", fileName); Process process = Process.Start("notepad.exe", fileName); guard.Register(Blocking.Task(KillProcess, Tuple.Create(process, traceSource))); traceSource.TraceInformation("Waiting..."); await Task.Delay(3000, token); ++index; } }