/// <summary> /// Acquires the reader /// If there is no convention the readers will just continue reading for as /// long as they need to /// </summary> public void AcquireReader() { // ---------------------- #1 TS ------------------------------------------------------- // Should be signed if there there is something waiting and should be released when the last // thread has finished with the LS readerTS.Acquire(); readerTS.Release(); lock (lockObject) { ++readersWaiting; // Used to figure out when the last reader has finished with the LS } // ------------------------------------------------------------------------------------- LS.Acquire(); // << ADDS READER TO LS lock (lockObject) { --readersWaiting; // If readers waiting then pulse if (readersWaiting != 0) { Monitor.Pulse(lockObject); } } }
/// <summary> /// 绘制 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MonitorUserControl_Paint(object sender, PaintEventArgs e) { if (_objRef == null) { return; } if (SM.TryEnter(_objRef)) { try { if (_bitmap == null) { return; } Point point = new Point(AutoScrollPosition.X, AutoScrollPosition.Y); e.Graphics.DrawImage(_bitmap, point); } finally { SM.Exit(_objRef); } } }
/// <summary> /// 屏幕 /// </summary> public void UpdateDisplay() { if (_objRef == null) { return; } if (SM.TryEnter(_objRef)) { try { byte[] bitmapBytes = _objRef.GetDesktopBitmapBytes(); if (bitmapBytes.IsNullOrEmpty()) { return; } var stream = new MemoryStream(bitmapBytes, false); _bitmap = (Bitmap)Image.FromStream(stream); Point point = new Point(AutoScrollPosition.X, AutoScrollPosition.Y); CreateGraphics().DrawImage(_bitmap, point); } finally { SM.Exit(_objRef); } } }
private void PutTask() { lock (this.SyncObject1) { this.Value = "put"; Monitor.Pulse(this.SyncObject1); } }
private void TakeTask() { lock (this.SyncObject1) { if (this.Value != "put") { Monitor.Wait(this.SyncObject1); } this.Value = "taken"; } }
/// <summary> /// Releases the writer, another writer my continue or all the readers may start reading /// </summary> public void ReleaseWriter() { writePermission.Release(); // >> REMOVES WRITER FROM WRITE QUEUE lock (lockObject) { if (readersWaiting > 0) { Monitor.Wait(lockObject); } } writerTS.Release(); }
public Random GetNamedRandom(string name, long seed) { try { ThreadMonitor.Enter(LockNamedDict); if (!NamedPRNGs.ContainsKey(name)) { NamedPRNGs.Add(name, GetNewRandom(seed)); } } finally { ThreadMonitor.Exit(LockNamedDict); } return(NamedPRNGs[name]); }
public void TestMonitorWithLockTaken() { this.Test(() => { object obj = new object(); bool lockTaken = false; Monitor.TryEnter(obj, ref lockTaken); if (lockTaken) { Monitor.Exit(obj); } Specification.Assert(lockTaken, "lockTaken is false"); }, GetConfiguration()); }
private bool RemoveCore(WaitNode node) { Debug.Assert(Monitor.IsEntered(this)); var inList = false; if (ReferenceEquals(first, node)) { first = node.Next; inList = true; } if (ReferenceEquals(last, node)) { last = node.Previous; inList = true; } inList |= node.IsNotRoot; node.Detach(); return(inList); }
public void RequestExitAndWait() { // don't call this from GLThread thread or it is a guaranteed // deadlock! //synchronized (this) { lock (locker) { this.mDone = true; //this.notify(); Monitor.Pulse(locker); } try { //this.join(); //Thread.CurrentThread().Join(); System.Threading.Monitor.Enter(locker); } catch (InterruptedException ex) { Java.Lang.Thread.CurrentThread().Interrupt(); } // TODO: Added this call ... check it finally { System.Threading.Monitor.Exit(locker); } }
public static void Exit(object obj) { SMonitor.Exit(obj); }
public static bool TryEnter(object obj) { return(SMonitor.TryEnter(obj)); }
public static bool Wait(object obj, int timeout) { return(SMonitor.Wait(obj, timeout)); }
public static void Wait(object obj) { SMonitor.Wait(obj); }
public static void PulseAll(object obj) { SMonitor.PulseAll(obj); }
public static void Enter(object obj) { SMonitor.Enter(obj); }
public static bool TryEnter(object obj, TimeSpan timeout) { return(SMonitor.TryEnter(obj, timeout)); }