Ticket #2061: asynchronouscontentview.patch
File asynchronouscontentview.patch, 5.3 KB (added by gkronber, 10 years ago) |
---|
-
HeuristicLab.MainForm.WindowsForms/3.3/Views/AsynchronousContentView.cs
52 52 protected new void Invoke(Delegate method, params object[] args) { 53 53 // prevents blocking of worker thread in Invoke, if the control is disposed 54 54 IAsyncResult result = BeginInvoke(method, args); 55 result.AsyncWaitHandle.WaitOne(10 00, false);55 result.AsyncWaitHandle.WaitOne(10, false); 56 56 if (result.IsCompleted) try { EndInvoke(result); } 57 57 catch (ObjectDisposedException) { } else { 58 58 ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, -
HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs
23 23 using System.Collections.Generic; 24 24 using System.Drawing; 25 25 using System.Linq; 26 using System.Threading; 26 27 using HeuristicLab.Collections; 27 28 using HeuristicLab.Common; 28 29 using HeuristicLab.Core; … … 179 180 experimentStopped = false; 180 181 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) { 181 182 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 182 try { optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { } 183 try { optimizer.Prepare(clearRuns); } 184 catch (InvalidOperationException) { } 183 185 } 184 186 } 185 187 public void Start() { … … 192 194 IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)); 193 195 if (optimizer != null) { 194 196 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 195 try { optimizer.Start(); } catch (InvalidOperationException) { } 197 try { optimizer.Start(); } 198 catch (InvalidOperationException) { } 196 199 } 197 200 } 198 201 public void Pause() { … … 204 207 experimentStopped = false; 205 208 foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) { 206 209 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 207 try { optimizer.Pause(); } catch (InvalidOperationException) { } 210 try { optimizer.Pause(); } 211 catch (InvalidOperationException) { } 208 212 } 209 213 } 210 214 public void Stop() { … … 217 221 if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) { 218 222 foreach (var optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) { 219 223 // a race-condition may occur when the optimizer has changed the state by itself in the meantime 220 try { optimizer.Stop(); } catch (InvalidOperationException) { } 224 try { optimizer.Stop(); } 225 catch (InvalidOperationException) { } 221 226 } 222 227 } else { 223 228 OnStopped(); … … 340 345 } 341 346 342 347 private readonly object locker = new object(); 348 private readonly object runsLocker = new object(); 343 349 private void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) { 344 350 lock (locker) 345 351 OnExceptionOccurred(e.Value); 346 352 } 347 353 private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) { 348 lock (locker) 349 ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime); 354 // only wait for maximally 100ms to acquire lock, otherwise return and don't update the execution time 355 var success = Monitor.TryEnter(locker, 100); 356 if (!success) return; 357 ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime); 358 Monitor.Exit(locker); 350 359 } 351 360 private void optimizer_Paused(object sender, EventArgs e) { 352 361 lock (locker) … … 357 366 if (Optimizers.All(x => x.ExecutionState == ExecutionState.Prepared)) OnPrepared(); 358 367 } 359 368 private void optimizer_Started(object sender, EventArgs e) { 369 if (ExecutionState == ExecutionState.Started) return; 360 370 lock (locker) 361 371 if (ExecutionState != ExecutionState.Started) OnStarted(); 362 372 } … … 373 383 } 374 384 } 375 385 private void optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) { 376 lock ( locker) {386 lock (runsLocker) { 377 387 Runs.RemoveRange(e.OldItems); 378 388 Runs.AddRange(e.Items); 379 389 } 380 390 } 381 391 private void optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) { 382 lock ( locker)392 lock (runsLocker) 383 393 Runs.AddRange(e.Items); 384 394 } 395 385 396 private void optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) { 386 lock ( locker)397 lock (runsLocker) 387 398 Runs.RemoveRange(e.Items); 388 399 } 389 400