Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I created a class I called <code>SuspendLatch</code>. Offers on a better name are welcome, but it does what you need and you would use it like this:</p> <pre><code>void Method() { using (suspendLatch.GetToken()) { // Update selected index etc } } void listbox1_SelectedIndexChanged(object sender, EventArgs e) { if (suspendLatch.HasOutstandingTokens) { return; } // Do some work } </code></pre> <p>It's not pretty, but it does work, and unlike unregistering events or boolean flags, it supports nested operations a bit like TransactionScope. You keep taking tokens from the latch and it's only when the last token is disposed that the <code>HasOutstandingTokens</code> returns false. Nice and safe. Not threadsafe, though...</p> <p>Here's the code for SuspendLatch:</p> <pre><code>public class SuspendLatch { private IDictionary&lt;Guid, SuspendLatchToken&gt; tokens = new Dictionary&lt;Guid, SuspendLatchToken&gt;(); public SuspendLatchToken GetToken() { SuspendLatchToken token = new SuspendLatchToken(this); tokens.Add(token.Key, token); return token; } public bool HasOutstandingTokens { get { return tokens.Count &gt; 0; } } public void CancelToken(SuspendLatchToken token) { tokens.Remove(token.Key); } public class SuspendLatchToken : IDisposable { private bool disposed = false; private Guid key = Guid.NewGuid(); private SuspendLatch parent; internal SuspendLatchToken(SuspendLatch parent) { this.parent = parent; } public Guid Key { get { return this.key; } } public override bool Equals(object obj) { SuspendLatchToken other = obj as SuspendLatchToken; if (other != null) { return Key.Equals(other.Key); } else { return false; } } public override int GetHashCode() { return Key.GetHashCode(); } public override string ToString() { return Key.ToString(); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { // Dispose managed resources. parent.CancelToken(this); } // There are no unmanaged resources to release, but // if we add them, they need to be released here. } disposed = true; // If it is available, make the call to the // base class's Dispose(Boolean) method //base.Dispose(disposing); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload