Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/26/17 15:48:32 (7 years ago)
Author:
jkarder
Message:

#2258: merged Async branch into trunk

Location:
trunk/sources
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources

  • trunk/sources/HeuristicLab.Algorithms.Benchmarks/3.3/BenchmarkAlgorithm.cs

    r14185 r15287  
    302302    }
    303303    public void Start() {
    304       cancellationTokenSource = new CancellationTokenSource();
     304      Start(CancellationToken.None);
     305    }
     306    public void Start(CancellationToken cancellationToken) {
     307      cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
    305308      OnStarted();
    306       Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
    307       task.ContinueWith(t => {
    308         try {
    309           t.Wait();
    310         }
    311         catch (AggregateException ex) {
    312           try {
    313             ex.Flatten().Handle(x => x is OperationCanceledException);
    314           }
    315           catch (AggregateException remaining) {
    316             if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
    317             else OnExceptionOccurred(remaining);
    318           }
    319         }
    320 
    321         cancellationTokenSource.Dispose();
    322         cancellationTokenSource = null;
    323         OnStopped();
    324       });
     309
     310      try {
     311        Run(cancellationTokenSource.Token);
     312      } catch (OperationCanceledException) {
     313      } catch (AggregateException ae) {
     314        OnExceptionOccurred(ae.InnerExceptions.SingleOrDefault() ?? ae);
     315      } catch (Exception e) {
     316        OnExceptionOccurred(e);
     317      }
     318
     319      cancellationTokenSource.Dispose();
     320      cancellationTokenSource = null;
     321      OnStopped();
     322    }
     323    public async Task StartAsync() { await StartAsync(CancellationToken.None); }
     324    public async Task StartAsync(CancellationToken cancellationToken) {
     325      await AsyncHelper.DoAsync(Start, cancellationToken);
    325326    }
    326327
     
    346347        Benchmark.TimeLimit = timelimit;
    347348        Benchmark.Run(cancellationToken, results);
    348       }
    349       catch (OperationCanceledException) {
    350       }
    351       finally {
     349      } catch (OperationCanceledException) {
     350      } finally {
    352351        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
    353352        timer.Stop();
Note: See TracChangeset for help on using the changeset viewer.