D
Doug Thews
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 ReaderWriterLocks 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
AcquireWriterLock method):
----------
myLock.AcquireWriterLock(1000);
iasResult = fprUIUpdate.BeginInvoke(strBuffer,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.EndInvoke(iasResult);
myLock.ReleaseWriterLock();
---------
But, if I do it this way, the WriterLock never gets cleared, even though the
statement gets executed in the AsyncCallback method:
---------
myLock.AcquireWriterLock(1000);
iasResult = fprUIUpdate.BeginInvoke(strBuffer,new
AsyncCallback(MessageUpdatedCallBack),null);
// Some other async operations can be done here
public void MessageUpdatedCallBack(System.IAsyncResult Result)
{
// Get the results of the delegate using EndInvoke
fprUIUpdate.EndInvoke(iasResult);
// Operation completed, we can now give up the buffer lock
myLock.ReleaseWriterLock();
}
---------
I've validated that the myLock.ReleaseWriterLock 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.
example, I call BeginInvoke and pass it an AsyncCallback function pointer.
I was messing around with ReaderWriterLocks 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
AcquireWriterLock method):
----------
myLock.AcquireWriterLock(1000);
iasResult = fprUIUpdate.BeginInvoke(strBuffer,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.EndInvoke(iasResult);
myLock.ReleaseWriterLock();
---------
But, if I do it this way, the WriterLock never gets cleared, even though the
statement gets executed in the AsyncCallback method:
---------
myLock.AcquireWriterLock(1000);
iasResult = fprUIUpdate.BeginInvoke(strBuffer,new
AsyncCallback(MessageUpdatedCallBack),null);
// Some other async operations can be done here
public void MessageUpdatedCallBack(System.IAsyncResult Result)
{
// Get the results of the delegate using EndInvoke
fprUIUpdate.EndInvoke(iasResult);
// Operation completed, we can now give up the buffer lock
myLock.ReleaseWriterLock();
}
---------
I've validated that the myLock.ReleaseWriterLock 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.