Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2410


Ignore:
Timestamp:
10/05/09 17:24:38 (15 years ago)
Author:
epitzer
Message:

make CancelAll asynchronous, add support for several worker monitors. (#769)

Location:
trunk/sources/HeuristicLab.BackgroundProcessing/3.2
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.BackgroundProcessing/3.2/ObservableBackgroundWorker.cs

    r2408 r2410  
    3737    public event PropertyChangedEventHandler PropertyChanged;
    3838
    39     public ObservableBackgroundWorker(string name) {
     39    public ObservableBackgroundWorker(string name, WorkerMonitor monitor) {
    4040      Name = name;
    41       WorkerMonitor.Default.RegisterWorker(this);
     41      monitor.RegisterWorker(this);
    4242      IsRunning = false;
    4343    }
     44
     45    public ObservableBackgroundWorker(string name) : this(name, WorkerMonitor.Default) { }
    4446
    4547    protected override void OnProgressChanged(ProgressChangedEventArgs e) {
  • trunk/sources/HeuristicLab.BackgroundProcessing/3.2/ObservableEnumerable.cs

    r2408 r2410  
    1010  /// </summary>
    1111  /// <typeparam name="T"></typeparam>
    12   public interface ObservableEnumerable<T> : IEnumerable<T>, INotifyCollectionChanged where T : class { }
     12  public interface IObservableEnumerable<T> : IEnumerable<T>, INotifyCollectionChanged { }
    1313}
  • trunk/sources/HeuristicLab.BackgroundProcessing/3.2/WorkerMonitor.cs

    r2408 r2410  
    1313  /// Provides a list of all currently running or pending ObservableBackgroundWorkers.
    1414  /// </summary>
    15   public class WorkerMonitor : ObservableEnumerable<ObservableBackgroundWorker> {
     15  public class WorkerMonitor : IObservableEnumerable<ObservableBackgroundWorker> {
     16
    1617    public static WorkerMonitor Default = new WorkerMonitor();
    1718
    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;
    1926    public event NotifyCollectionChangedEventHandler CollectionChanged;
    2027
     
    7279
    7380    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));
    7683    }
    7784
     
    95102    }
    96103
    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);
    110125        }
    111         while (cancelableWorkers.Count > 0)
    112           Monitor.Wait(cancelableWorkers);
    113       }
     126        if (callback != null)
     127          callback(state);
     128      });
    114129    }
    115130
Note: See TracChangeset for help on using the changeset viewer.