public static void Main(String[] args) { // Configure a logger to capture the output of the library Tracer.Trace = new ConsoleLogger(); try { using (var subscriber = new Subscriber()) { SendMessages(); Console.WriteLine($" [{Thread.CurrentThread.ManagedThreadId}] Start receiving messages."); subscriber.Start(); Console.WriteLine(ResetEvent.Wait(1.ToMinutes()) ? "All messages received" : "Timeout :("); } } catch (Exception ex) { Console.WriteLine($"Error: {ex}"); } Console.WriteLine("Press <enter> to exit."); Console.ReadLine(); }
/// <summary> /// /// </summary> /// <param name="timeout"></param> /// <returns></returns> public bool Wait(int timeout) { if (IsCompleted) { return(true); } if (Canceled) { return(true); } CheckNotDisposed(); if (resetEvent == null) { resetEvent = new ResetEvent(false); } return(resetEvent.Wait(timeout)); }
public void DoWorkTest_WaitUntilCompleted() { // Arrange WorkType = WorkType.GenerateReports; WorkHours = 8; // Act Task.Run(() => Worker.StartWork(WorkType, WorkHours)); // Block Current Thread ResetEvent.Wait(); }
async Task <(int s, int r)> retry(ResetEvent rst, int awaitMS) { var retries = 0; var state = -1; while (true) { state = await rst.Wait(awaitMS); if (state < 1) { retries++; } else { break; } } return(s : state, retries); }
public void ThreadLifestyleTest() { ServiceRegistry.Current.Register <IPerson, Person>("person", LifestyleFlags.Thread); IPerson person = null, person2 = null, person3 = null, person4 = null; PopulatePerThreadLifestyle(ref person, ref person2); var mre = new ResetEvent(false); ThreadPool.QueueUserWorkItem((s) => { PopulatePerThreadLifestyle(ref person3, ref person4); mre.Set(); }); mre.Wait(); Assert.AreSame(person, person2); Assert.AreSame(person3, person4); Assert.AreNotSame(person, person3); }
public void GenericThreadLifestyleTest() { ServiceRegistry.Current.Register(new ComponentInfo(null, typeof(IList <>), typeof(List <>), LifestyleFlags.Thread)); IList <int> coll = null, coll2 = null, coll3 = null, coll4 = null; PopulatePerThreadLifestyle(ref coll, ref coll2); var mre = new ResetEvent(false); ThreadPool.QueueUserWorkItem((s) => { PopulatePerThreadLifestyle(ref coll3, ref coll4); mre.Set(); }); mre.Wait(); Assert.AreSame(coll, coll2); Assert.AreSame(coll3, coll4); Assert.AreNotSame(coll, coll3); }
async Task WithCheck(TestArgs targ) { const int PULSE_RETENTION = 0; $"WithCheck, PulseRetentionMS: {PULSE_RETENTION}".AsInfo(); Beamer rayA = null; Beamer rayB = null; try { var done = new ResetEvent(false); var aep = targ.AE; var bep = targ.BE; const int BYTES_TO_TRANSFER = 10_000_000; const int MAX_RANDOM_SIZE = 1000; int totalSent = 0; int totalReceived = 0; rayA = new Beamer( (f) => { Console.WriteLine("?"); }, new BeamerCfg() { EnablePulsing = true, PulseRetentionMS = 0, Log = new BeamerLogCfg("rayA-PulseWithCheck", targ.Log) }); rayB = new Beamer((f) => { try { if (f == null || f.IsDisposed || f.Length < 1) { throw new Exception("Frag"); } var fs = f.Span(); var v = fs[0]; for (int i = 1; i < fs.Length; i++) { if (v != fs[i] || fs[i] == 0) { FailureMessage = "Received wrong data"; Passed = false; done.Set(false); } } var tr = Interlocked.Add(ref totalReceived, f.Length); //$"TR: {tr}".AsInfo(); if (tr >= BYTES_TO_TRANSFER) { done.Set(); } } catch (Exception ex) { ex.ToString().AsError(); } finally { if (f != null) { f.Dispose(); } } }, new BeamerCfg() { Log = new BeamerLogCfg("rayB-PulseWithCheck", targ.Log) }); using (var hw = new HeapHighway()) { var ta = new Task(async() => { await Task.Yield(); rayA.LockOn(aep, bep); var rdm = new Random(); while (true) { try { if (done.Task.Status == TaskStatus.RanToCompletion) { return; } using (var f = hw.Alloc(rdm.Next(1, MAX_RANDOM_SIZE))) { var v = (byte)rdm.Next(1, 255); for (int j = 0; j < f.Length; j++) { f[j] = v; } rayA.Pulse(f); var ts = Interlocked.Add(ref totalSent, f.Length); if (ts >= BYTES_TO_TRANSFER) { break; } } } catch (Exception ex) { } } }); var tb = new Task(() => { rayB.LockOn(bep, aep); }); ta.Start(); tb.Start(); if (await done.Wait() < 0) { Passed = false; FailureMessage = "Timeout"; } } if (!Passed.HasValue) { "OK: WithCheck".AsSuccess(); } } catch (Exception ex) { FailureMessage = ex.Message; Passed = false; } finally { rayA.Dispose(); rayB.Dispose(); } }
async Task NoCheck(TestArgs targ) { int PULSE_RETENTION = new BeamerCfg().PulseRetentionMS; $"WithoutCheck, PulseRetentionMS: {PULSE_RETENTION}".AsInfo(); Beamer rayA = null; Beamer rayB = null; try { var done = new ResetEvent(false); var aep = targ.AE; var bep = targ.BE; const int BYTES_TO_TRANSFER = 100_000_000; const int MAX_RANDOM_SIZE = 1000; int totalSent = 0; int totalReceived = 0; rayA = new Beamer((f) => { }, new BeamerCfg() { EnablePulsing = true, Log = new BeamerLogCfg("rayA-PulseNoCheck", targ.Log) }); rayB = new Beamer((f) => { try { if (f == null || f.IsDisposed || f.Length < 1) { throw new Exception("Frag"); } var tr = Interlocked.Add(ref totalReceived, f.Length); if (tr >= BYTES_TO_TRANSFER) { done.Set(); } } catch (Exception ex) { ex.ToString().AsError(); } finally { if (f != null) { f.Dispose(); } } }, new BeamerCfg() { Log = new BeamerLogCfg("rayB-PulseNoCheck", targ.Log) }); var hw = new HeapHighway(); var ta = new Task(async() => { await Task.Yield(); rayA.LockOn(aep, bep); var rdm = new Random(); while (true) { try { if (done.Task.Status == TaskStatus.RanToCompletion) { return; } using (var f = hw.Alloc(rdm.Next(1, MAX_RANDOM_SIZE))) { rayA.Pulse(f); var ts = Interlocked.Add(ref totalSent, f.Length); if (ts >= BYTES_TO_TRANSFER) { break; } } } catch (Exception ex) { ex.Message.AsError(); } } rayA.trace("Out of pulsing", null); }); var tb = new Task(async() => { rayB.LockOn(bep, aep); }); ta.Start(); tb.Start(); if (await done.Wait() < 0) { Passed = false; FailureMessage = "Timeout"; } if (!Passed.HasValue) { "OK: WithoutCheck".AsSuccess(); } } catch (Exception ex) { FailureMessage = ex.Message; Passed = false; } finally { rayA.Dispose(); rayB.Dispose(); } }