ReaderWriterLockSlim + Dispose?

N

NvrBst

class Program {
static Class1 test = new Class1();
static void Main(string[] args) {
new Thread(() => test.StartWork()).Start();
test.Dispose();
Thread.Sleep(1000);
}
}
class Class1 {
private ReaderWriterLockSlim myStringLock = new
ReaderWriterLockSlim();
private int _cthread;
private volatile bool _fDispose;
public void StartWork() {
Thread.Sleep(200);

_cthread = 2;
ThreadPool.QueueUserWorkItem(new WaitCallback(test));
ThreadPool.QueueUserWorkItem(new WaitCallback(test));
}
public void test(Object nullState) {
myStringLock.EnterWriteLock();
foreach(string A in new string[] { "hello", "goodbye" })
Console.WriteLine(A);
myStringLock.ExitWriteLock();
if(Interlocked.Decrement(ref _cthread) == 0 && _fDispose)
Dispose(true);
}
private int disposed = 0;
public void Dispose() {
_fDispose = true;
if(_cthread == 0) Dispose(true);
}
protected virtual void Dispose(bool disposing)
{ if(Interlocked.Exchange(ref disposed, 1) == 0) if(disposing)
myStringLock.Dispose(); }
}


Object Disposed exception at "myStringLock.EnterWriteLock();", I've
only added a "Thread.Sleep(200);" to illustrate.

I do appologize if I hurt you. I think I might of been missreading
your tone, since you seem to be all over the place. One moment you
seem to be in a mud pit with your "can't admit when your wrong,
complete newbie in the field, etc, etc" just to name a few, then
others you seem to be civil. Either way you do have a "my crap don't
stink, but yours do" feel which doesn't sit well with me.

NB
 
N

NvrBst

There is a difference between
..Dispose()
..StartWork()

and

..StartWork()
..Dispose()


You seem to fail to realize that the StartWork method is 100% getting
called before Dispose. The Inital implementation allows this, you
would have to add more code to fix this situation to give the 2
classes a better coparison. You seem to think your god and can say a
thread can't call StartWork, right before another thread says
"Dispose, we are done"? You think this isn't possible? Its the exact
same situation as your "event" method, just at a different part.
I only hope that in time, you will develop enough maturity to be
able to recognize your own mistakes when they are pointed out to you,
rather than becoming so intensely defensive, blinded by your own pride.

I was going to say the same about you! ;) But then you started crying
about ad homminims, and I felt sorry for you.

NB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top