/// <summary> /// 发送数据消费者,每隔50ms读取队列数据,通过USB发送数据 /// </summary> public void UsbTxStart() { latch.Acquire(); while (NeedRunning) { var list = txQueue.PopAll(); foreach (var np in list) { try { var data = np.encode(); int len = data.Length; usb.sendDataSynchronous(ref data, ref len); LogHelper.GetLogger <ProcThread>().Debug("Send Data: " + ByteHelper.Byte2ReadalbeXstring(data)); } catch (Exception ex) { LogHelper.GetLogger <ProcThread>().Error(string.Format("Send data Error. Reason: {0}, Detail: {1}.", ex.ToString(), ex.StackTrace)); } } if (!NeedRunning) { break; } Thread.Sleep(50); } }
public void UsesEventFiltersToAllowAnEventToPropagated() { MockFileSystemEventFilter filter = new MockFileSystemEventFilter(true); watcher.StartWatching(dispatcher); watcher.AddAllowFilter(filter); string subDir = Path.Combine(appFullPath, "foo"); Directory.CreateDirectory(subDir); using (File.Create(Path.Combine(appFullPath, "foo.bar"))) {} eventLatch.Acquire(); Assert.IsTrue(filter.called); }
public void WaitOnBorrowWhenExausted() { int n = 100; object[] objects = new object[n]; pool = new SimplePool(new MyFactory(), n); for (int i = 0; i < n; i++) { objects[i] = pool.BorrowObject(); } Latch latch = new Latch(); ISync sync = new Latch(); Helper helper = new Helper(latch, sync, pool); Thread thread = new Thread(new ThreadStart(helper.UsePool)); thread.Start(); Assert.AreEqual(n, pool.NumActive); Assert.AreEqual(0, pool.NumIdle); object released = objects[n - 1]; pool.ReturnObject(released); latch.Acquire(); Assert.AreEqual(n, pool.NumActive); Assert.AreEqual(0, pool.NumIdle); Assert.IsNotNull(helper.gotFromPool, "helper did not get from pool"); Assert.AreSame(released, helper.gotFromPool, "got unexpected object"); }
public static void OtherThread() { Console.WriteLine("Is waiting for latch to be released!"); latchOne.Acquire(); // Shows that all the waiting threads will be released and that they are blocked with Acquire Console.WriteLine("Latch has been released!"); }
public static void TestLatchAcquire() { while (true) { Thread.Sleep(1000); Console.WriteLine(Thread.CurrentThread.Name + " is going to attempt to test latch acquire!"); _testLatch.Acquire(); Console.WriteLine("\t" + Thread.CurrentThread.Name + " completed the test and a token has been immediately released!"); Thread.Sleep(1000); } }
public void WaitForStartedBeforeStopping() { MyService serviceable = new MyService(sync1, sync2); ISync starting = new Latch(); ISync started = new Latch(); BlockWhileStartingExecutor executor = new BlockWhileStartingExecutor(starting, started); MyServiceSupport support = new MyServiceSupport(executor, serviceable); executor.ServiceSupport = support; Thread startThread = new Thread(new ThreadStart(executor.Start)); startThread.Name = "start"; startThread.Start(); Log("start thread started"); Latch stopping = new Latch(); Latch stopped = new Latch(); StoppingHelper helper = new StoppingHelper(support, stopping, stopped); Thread stopThread = new Thread(new ThreadStart(helper.Stop)); stopThread.Name = "stop"; stopThread.Start(); Log("stop thread started: waiting for stopping ..."); stopping.Acquire(); Log("stopping in progress ..."); Assert.IsFalse(executor.wasStarted); Assert.IsFalse(helper.wasStopped, "helper could stop before expected"); Log("allow to start ..."); starting.Release(); Log("waiting for started ..."); started.Acquire(); Assert.IsTrue(executor.wasStarted); stopped.Acquire(); Log("waiting for stop ..."); Assert.IsTrue(helper.wasStopped); Log("stopped ..."); }
/// <summary> /// Waits for the latch to be released and prints messages indicating the status of the latch /// </summary> private void WaitForLatch() { Console.WriteLine(Thread.CurrentThread.Name + ": Waiting for latch..."); _latch.Acquire(); Console.WriteLine(Thread.CurrentThread.Name + ": Latch released!"); }