public static void AddToQueueAndWaitExecution(this IQueueWorker <Action> queueWorker, Action a) { var signal = new ManualResetEvent(false); Exception exception = null; queueWorker.AddToExecutionQueue(() => { try { a(); } catch (Exception ex) { exception = ex; } finally { signal.Set(); } }); signal.WaitOne(); if (exception != null) { throw exception; } }
private void QueueBackgroundWorkerTest() { Log("QueueBackgroundWorkerTest"); var bw = new QueueBackWorker <Action>(action => action()); _queueWorker = bw; _uiNotifier = bw; for (int i = 0; i < 10; ++i) { var i1 = i; _queueWorker.AddToExecutionQueue(() => { Log("Hello" + i1); _uiNotifier.Notify(() => Log("World" + i1)); }); } }
public static void AddToQueueAndWaitExecution(this IQueueWorker <Action> queueWorker, Action a, TimeSpan timeout) { var sync = new object(); var signal = new ManualResetEvent(false); bool wasExecuted = false; Exception exception = null; queueWorker.AddToExecutionQueue(() => { try { a(); lock (sync) { wasExecuted = true; } } catch (Exception ex) { exception = ex; } finally { signal.Set(); } }); signal.WaitOne(timeout); bool hasBeenExecuted; lock (sync) { hasBeenExecuted = wasExecuted; } if (!hasBeenExecuted) { throw new Exception("Таймаут операции"); } if (exception != null) { throw exception; } }