I've been working on some samples that use BeginInvoke/EndInvoke. In one
example, I call BeginInvoke and pass it an AsyncCallback function pointer.
I was messing around with ReaderWriterLoc ks and noticed that if I did this,
it worked (please ignore the lack of try ... catch blocks, because I cut
down the code to be more brief - I have a try...catch surround the
AcquireWriterLo ck method):
----------
myLock.AcquireW riterLock(1000) ;
iasResult = fprUIUpdate.Beg inInvoke(strBuf fer,null,null);
// Some other async operations can be done here
// Get the results of the delegate using EndInvoke. This will block
// execution of this thread until the delegate invoked completes
fprUIUpdate.End Invoke(iasResul t);
myLock.ReleaseW riterLock();
---------
But, if I do it this way, the WriterLock never gets cleared, even though the
statement gets executed in the AsyncCallback method:
---------
myLock.AcquireW riterLock(1000) ;
iasResult = fprUIUpdate.Beg inInvoke(strBuf fer,new
AsyncCallback(M essageUpdatedCa llBack),null);
// Some other async operations can be done here
public void MessageUpdatedC allBack(System. IAsyncResult Result)
{
// Get the results of the delegate using EndInvoke
fprUIUpdate.End Invoke(iasResul t);
// Operation completed, we can now give up the buffer lock
myLock.ReleaseW riterLock();
}
---------
I've validated that the myLock.ReleaseW riterLock method gets called in the
AsyncCallback routine, but for some reason the very next time it gets
checked, it comes back as locked. BUT, if I move it all back into the
thread and don't use callbacks, the same logic seems to work fine.
Is myLock no longer the same object reference (even though it's all in the
same class) because it's now inside an AsyncCallback function? I made
myLock a static variable inside the class to make sure that this wasn't the
problem, but no luck.
--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/