コード例 #1
0
ファイル: Program.cs プロジェクト: sujit1779/writeasync
        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;
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: knightfall/writeasync
        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;
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: sujit1779/writeasync
        private static void Main(string[] args)
        {
            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                TraceSource traceSource = new TraceSource("CleanupSample", SourceLevels.All);
                traceSource.Listeners.Add(new ConsoleTraceListener()
                {
                    TraceOutputOptions = TraceOptions.DateTime | TraceOptions.ThreadId
                });

                CleanupGuard guard = new CleanupGuard();
                Task         task  = guard.RunAsync(g => RunSampleAsync(g, traceSource, cts.Token));

                Console.WriteLine("Press ENTER to stop.");
                Console.ReadLine();

                cts.Cancel();

                try
                {
                    task.Wait();
                }
                catch (Exception e)
                {
                    traceSource.TraceEvent(TraceEventType.Error, 0, "ERROR: {0}", e);
                }
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: knightfall/writeasync
        private static void Main(string[] args)
        {
            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                TraceSource traceSource = new TraceSource("CleanupSample", SourceLevels.All);
                traceSource.Listeners.Add(new ConsoleTraceListener() { TraceOutputOptions = TraceOptions.DateTime | TraceOptions.ThreadId });

                CleanupGuard guard = new CleanupGuard();
                Task task = guard.RunAsync(g => RunSampleAsync(g, traceSource, cts.Token));

                Console.WriteLine("Press ENTER to stop.");
                Console.ReadLine();

                cts.Cancel();

                try
                {
                    task.Wait();
                }
                catch (Exception e)
                {
                    traceSource.TraceEvent(TraceEventType.Error, 0, "ERROR: {0}", e);
                }
            }
        }