public void Repeat(string name, Action action, Action<Exception> onException, int interval = 100, ThreadPriority priority = ThreadPriority.Normal, CancellationToken? cancellationToken = null) { var thread = new Thread( () => { try { while (true) { if (cancellationToken.HasValue && cancellationToken.Value.IsCancellationRequested) break; Sleep(interval); action(); } } catch (Exception e) { onException(e); } } ); thread.Run(priority); Register(thread, cancellationToken, name); }
public void ThreadWaitTillAllCompleteTest() { counter = 0; Thread[] threads = new Thread[100]; for(int i = 0; i < threads.Length; i++) { threads[i] = new Thread(() => { Thread.SleepInSeconds(Random.Uint.LessThan(3)); Interlocked.Increment(ref counter); }); } threads.Run(); Assert.IsTrue(counter == threads.Length); }
public void Run(string name, Action action, Action<Exception> onException, ThreadPriority priority = ThreadPriority.Normal, CancellationToken? cancellationToken = null) { var thread = new Thread( () => { try { if (cancellationToken.HasValue && cancellationToken.Value.IsCancellationRequested) return; action(); } catch (Exception e) { onException(e); } } ); thread.Run(priority); Register(thread, cancellationToken, name); }