I have two methods, MethodA
& MethodB
. MethodB
has to run on the UI thread. I need them to run one after the other without allowing MethodC
to run between them.
MethodC
is called when a user clicks on a lovely little button.
What I did to ensure this is put a Lock
around the code thus:
lock (MyLock)
{
MethodA(param1, param2);
MyDelegate del = new MyDelegate(MethodB);
if (this.IsHandleCreated) this.Invoke(del);
}
And for MethodC
:
public void MethodC()
lock (MyLock)
{
Do bewildering stuff.....
}
Problem is I'm getting stuck. It looks like my code's going into a deadlock.
When I look at the threads I see that the code called by the button click is stuck at lock (MyLock)
in MethodC
and my other thread seems stuck at this.Invoke(del)
.
I've read it's dangerous to invoke a method from within a Lock
but since I'm the one who wrote the code there and this seems to happen even with just a Thread.Sleep
I figure it's not the code that's getting me into trouble.
Why would the the Invoked method stop working?
Is it possibly waiting for the lock on methodC
to be released before going back to the original lock it was invoked from?