Changeset 2410
- Timestamp:
- 10/05/09 17:24:38 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.BackgroundProcessing/3.2
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.BackgroundProcessing/3.2/ObservableBackgroundWorker.cs
r2408 r2410 37 37 public event PropertyChangedEventHandler PropertyChanged; 38 38 39 public ObservableBackgroundWorker(string name ) {39 public ObservableBackgroundWorker(string name, WorkerMonitor monitor) { 40 40 Name = name; 41 WorkerMonitor.Default.RegisterWorker(this);41 monitor.RegisterWorker(this); 42 42 IsRunning = false; 43 43 } 44 45 public ObservableBackgroundWorker(string name) : this(name, WorkerMonitor.Default) { } 44 46 45 47 protected override void OnProgressChanged(ProgressChangedEventArgs e) { -
trunk/sources/HeuristicLab.BackgroundProcessing/3.2/ObservableEnumerable.cs
r2408 r2410 10 10 /// </summary> 11 11 /// <typeparam name="T"></typeparam> 12 public interface ObservableEnumerable<T> : IEnumerable<T>, INotifyCollectionChanged where T : class{ }12 public interface IObservableEnumerable<T> : IEnumerable<T>, INotifyCollectionChanged { } 13 13 } -
trunk/sources/HeuristicLab.BackgroundProcessing/3.2/WorkerMonitor.cs
r2408 r2410 13 13 /// Provides a list of all currently running or pending ObservableBackgroundWorkers. 14 14 /// </summary> 15 public class WorkerMonitor : ObservableEnumerable<ObservableBackgroundWorker> { 15 public class WorkerMonitor : IObservableEnumerable<ObservableBackgroundWorker> { 16 16 17 public static WorkerMonitor Default = new WorkerMonitor(); 17 18 18 public event ThreadExceptionEventHandler ThreadException; 19 /// <summary> 20 /// Report all unhandled exceptions during background worker execution. 21 /// These exceptions are the same as reported by the RunWorkerCompleted but wrapped 22 /// in an additional exception containing the worker name as message and the original 23 /// exception as inner exception. 24 /// </summary> 25 public event ThreadExceptionEventHandler BackgroundWorkerException; 19 26 public event NotifyCollectionChangedEventHandler CollectionChanged; 20 27 … … 72 79 73 80 protected void OnThreadException(Exception x) { 74 if ( ThreadException != null)75 ThreadException(this, new ThreadExceptionEventArgs(x));81 if (BackgroundWorkerException != null) 82 BackgroundWorkerException(this, new ThreadExceptionEventArgs(x)); 76 83 } 77 84 … … 95 102 } 96 103 97 public void CancelAll() { 98 List<ObservableBackgroundWorker> cancelableWorkers = GetCancelableWorkers(); 99 lock (cancelableWorkers) { 100 foreach (var worker in cancelableWorkers.ToList()) { 101 worker.WorkerStopped += (sender, args) => { 102 lock (cancelableWorkers) { 103 cancelableWorkers.Remove((ObservableBackgroundWorker)sender); 104 Monitor.Pulse(cancelableWorkers); 105 } 106 }; 107 worker.CancelAsync(); 108 if (!worker.IsRunning) 109 cancelableWorkers.Remove(worker); 104 public void CancelAllAsync() { 105 CancelAllAsync(null, null); 106 } 107 108 public void CancelAllAsync(WaitCallback callback, object state) { 109 ThreadPool.QueueUserWorkItem(_ => { 110 List<ObservableBackgroundWorker> cancelableWorkers = GetCancelableWorkers(); 111 lock (cancelableWorkers) { 112 foreach (var worker in cancelableWorkers.ToList()) { 113 worker.WorkerStopped += (sender, args) => { 114 lock (cancelableWorkers) { 115 cancelableWorkers.Remove((ObservableBackgroundWorker)sender); 116 Monitor.Pulse(cancelableWorkers); 117 } 118 }; 119 worker.CancelAsync(); 120 if (!worker.IsRunning) 121 cancelableWorkers.Remove(worker); 122 } 123 while (cancelableWorkers.Count > 0) 124 Monitor.Wait(cancelableWorkers); 110 125 } 111 while (cancelableWorkers.Count > 0)112 Monitor.Wait(cancelableWorkers);113 } 126 if (callback != null) 127 callback(state); 128 }); 114 129 } 115 130
Note: See TracChangeset
for help on using the changeset viewer.