Note that there are some explanatory texts on larger screens.

plurals
  1. POIOException getting thrown by ReaderWriteLockSlim?
    primarykey
    data
    text
    <p>I have a static class which is accessed by multiple threads and uses a ReaderWriterLockSlim in various methods to maintain thread safety. This works fine most of the time, however very intermittently I'm seeing an IOException handle is invalid error being thrown by one particular method as shown in the stack trace below. This has me greatly confused as I didn't even know that System.IO was involved in a ReaderWriterLock.</p> <p>Any help at all in understanding where the error may be comming from would be greatly appreciated.</p> <p>Stack Trace:</p> <blockquote> <p>System.IO.IOException: The handle is invalid.<br> at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)<br> at System.Threading.EventWaitHandle.Reset()<br> at System.Threading.ReaderWriterLockSlim.WaitOnEvent(EventWaitHandle waitEvent, UInt32&amp; numWaiters, Int32 millisecondsTimeout)<br> at System.Threading.ReaderWriterLockSlim.TryEnterUpgradeableReadLock(Int32 millisecondsTimeout)</p> </blockquote> <p>Code:</p> <pre><code>class Class1 { private static ReaderWriterLockSlim readwriteLock = new ReaderWriterLockSlim(); private const int readwriterlocktimeoutms = 5000; private static void myMethod(int ID) { bool IsTaken = false; bool isWriteLockTaken = false; if (!readwriteLock.TryEnterUpgradeableReadLock(readwriterlocktimeoutms)) { throw new Exception("SafeGetSuItem: Error acquiring read lock"); } else { IsTaken = true; } try { // do some work which may require upgrading to a write lock depending on particular conditions } finally { if (IsTaken) { try { readwriteLock.ExitUpgradeableReadLock(); IsTaken = false; } catch { throw; } } } } } enter code here bool IsWriteTaken = false; try { if (!readerwriterlock.TryEnterWriteLock(readerwriterlocktimeout)) { // log the error } else { IsWriteTaken = true; } if (IsWriteTaken) { // do some work } } finally { if (IsWriteTaken) { try { readerwriterlock.ExitWriteLock(); } catch { throw; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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