App2.Program.Run C# (CSharp) Method

Run() public method

public Run ( CancellationToken cancellationToken, Action log ) : void
cancellationToken System.Threading.CancellationToken
log Action
return void
        public void Run(CancellationToken cancellationToken, Action<string> log)
        {
            log(string.Format("Started {0}", DateTime.Now));

            while (!cancellationToken.IsCancellationRequested)
            {
                log(string.Format("Timestamp {0}", DateTime.Now));

                cancellationToken.WaitHandle.WaitOne(5000);
            }

            log(string.Format("Stopped {0}", DateTime.Now));
        }

Usage Example

        static void Main(string[] args)
        {
            var program = new Program();

            var tokenSource = new CancellationTokenSource();
            Console.CancelKeyPress += (sender, eventArgs) =>
                {
                    // NOTE: This cancelling logic does not work in the VS2010 debugger with .Net 4, but does fine otherwise
                    tokenSource.Cancel();
                    eventArgs.Cancel = true;
                };

            program.Run(tokenSource.Token, Console.WriteLine);
        }