I lock
on a global object in order to synchronise communication to an external device from various threads, which is working fine.
I want to user a slider
control to set values which I then send to my external device, so I'm using its Scroll
event to listen for changes before sending them off to the device.
The problem is, I need to lock
on the global object before communicating with the device, as follows:
private void slider_Scroll(object sender, EventArgs e)
{
lock(lockObject)
{
//send slider values to external device
}
}
The above always hangs indefinitely on attempting to gain the lock
. The rest of my code has been working fine, so I don't think I have a genuine race condition - I'm assuming this is happening because it's running on the UI thread and that it's a Bad Idea to block on the UI thread? I could be wrong.
However, if this is the case, I'm guessing I need to wrap the above code in a method and somehow invoke it on the 'main' thread from the UI thread?
How can I achieve this?