private static void Tick4(World world, double elapsedTime) { world.Update(); }
private void btnThreadTimerWait_Click(object sender, RoutedEventArgs e) { try { MessageBox.Show("finish this", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning); return; if (_cancel3 == null) { _cancel3 = new ManualResetEvent(false); } Thread workerThread = new Thread(() => { // World World world = new World(false); world.UnPause(); #region Launch Timer // Put a timer in another thread ManualResetEvent elapsedEvent = new ManualResetEvent(false); System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = 25; timer.Elapsed += delegate { elapsedEvent.Set(); }; timer.AutoReset = false; timer.Start(); #endregion WaitHandle[] handles = new WaitHandle[] { _cancel3, elapsedEvent }; while (true) { #region Wait for signal to go int handleIndex = WaitHandle.WaitAny(handles); if (handleIndex == 0) { // This thread needs to finish break; // I would prefer a switch statement, but there is no Exit While statement } else if (handleIndex == 1) { // Timer ticked elapsedEvent.Reset(); timer.Start(); } else { throw new ApplicationException("Unknown wait handle: " + handleIndex.ToString()); } #endregion world.Update(); } timer.Stop(); world.Pause(); // pause really isn't necessary, since there isn't a timer to stop world.Dispose(); }); workerThread.Start(); //TODO: Figure out how to terminate the thread from this calling thread. This article says to keep looping and checking workerThread.IsAlive. //That's a mess. If that's the only way, then set up a little thread pool manager to do this //http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.90).aspx //task.ContinueWith(t => //{ // workerThread.Join(); // workerThread = null; // _cancel3 = null; // MessageBox.Show("Finished", this.Title, MessageBoxButton.OK, MessageBoxImage.Information); //}, TaskScheduler.FromCurrentSynchronizationContext()); } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// http://stackoverflow.com/questions/9853216/alternative-to-thread-sleep /// </summary> private void btnLongTimerWait_Click(object sender, RoutedEventArgs e) { try { if (_cancel2 == null) { _cancel2 = new CancellationTokenSource(); } CancellationToken cancelToken = _cancel2.Token; Task task = Task.Factory.StartNew(a => { // It is from the thread pool. Explicitly create a thread for cases like this instead of using task //if (Thread.CurrentThread.IsThreadPoolThread) //{ // throw new ApplicationException("Long running thread can't be from the thread pool"); //} // World World world = new World(false); world.UnPause(); #region Launch Timer // Put a timer in another thread ManualResetEvent elapsedEvent = new ManualResetEvent(false); System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = 25; timer.Elapsed += delegate { elapsedEvent.Set(); }; timer.AutoReset = false; timer.Start(); #endregion while (!cancelToken.IsCancellationRequested) { // Wait for the timer to tick and send a message elapsedEvent.WaitOne(); elapsedEvent.Reset(); timer.Start(); world.Update(); } timer.Stop(); world.Pause(); // pause really isn't necessary, since there isn't a timer to stop world.Dispose(); }, TaskCreationOptions.LongRunning, _cancel2.Token); task.ContinueWith(t => { _cancel2 = null; MessageBox.Show("Finished", this.Title, MessageBoxButton.OK, MessageBoxImage.Information); }, TaskScheduler.FromCurrentSynchronizationContext()); } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }
private void btnLongSleep0_Click(object sender, RoutedEventArgs e) { try { if (_cancel1 == null) { _cancel1 = new CancellationTokenSource(); } CancellationToken cancelToken = _cancel1.Token; Task task = Task.Factory.StartNew(a => { World world = new World(false); world.UnPause(); while (!cancelToken.IsCancellationRequested) { Thread.Sleep(0); world.Update(); } world.Pause(); // pause really isn't necessary, since there isn't a timer to stop world.Dispose(); }, TaskCreationOptions.LongRunning, _cancel1.Token); task.ContinueWith(t => { _cancel1 = null; MessageBox.Show("Finished", this.Title, MessageBoxButton.OK, MessageBoxImage.Information); }, TaskScheduler.FromCurrentSynchronizationContext()); } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }
private void btnTask100_Click(object sender, RoutedEventArgs e) { try { Task task = Task.Run(() => { World world = new World(false); world.UnPause(); for (int cntr = 0; cntr < 100; cntr++) { Thread.Sleep(50); world.Update(); } world.Pause(); // pause really isn't necessary, since there isn't a timer to stop world.Dispose(); }); task.ContinueWith(t => { MessageBox.Show("Finished", this.Title, MessageBoxButton.OK, MessageBoxImage.Information); }, TaskScheduler.FromCurrentSynchronizationContext()); } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }
private void btnUI100_Click(object sender, RoutedEventArgs e) { try { World world = new World(false); world.UnPause(); for (int cntr = 0; cntr < 100; cntr++) { Thread.Sleep(50); world.Update(); } world.Pause(); // pause really isn't necessary, since there isn't a timer to stop world.Dispose(); MessageBox.Show("Finished", this.Title, MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }