Object synchronization method was called from an unsynchronized block of code.
-
The following code throws the error above. Module Base Public sub Clock Do If needsNewChart = True Draw = New clsDraw("clock", i_id, True) thdWorkerSub = New Thread(AddressOf Draw.DoIt) thdWorkerSub.Name = "Draw" thdWorkerSub.Start() thdWorkerSub.Join() Draw = Nothing End If Thread.Sleep(100) Loop End Sub End Module Public Class clsDraw Public Sub DoIt '491 lines of code that creates a new bitMap(theBmp) and draws a stock market "tick chart". 'There are two uses of Monitor.Enter > Monitor.Exit on other objects without difficulties in this code. Try lockedGraphics = Monitor.TryEnter(theFrm.bmpTicks, 50) If lockedGraphics = True Then theFrm.bmpTicks = theBmp End If If lockedGraphics = True Then Monitor.Exit(theFrm.bmpTicks) End If Thread.Sleep(10) theFrm.pbTicks.Invalidate() Catch ex As Exception Ers.Show(c_id & "theTks.Draw.DoIt-setdown2", ex) End Try End Sub End Class Monitor.Exit(theFrm.bmpTicks) throws the error. When you step through the code in the debugger, the lock is acquired and the bitmap is copied. I use Monitor to synchronize my threads in many programs that work together to track the stock market. I have not encountered this error before. Why is it occuring? Thanks. RCarey
RCarey
-
The following code throws the error above. Module Base Public sub Clock Do If needsNewChart = True Draw = New clsDraw("clock", i_id, True) thdWorkerSub = New Thread(AddressOf Draw.DoIt) thdWorkerSub.Name = "Draw" thdWorkerSub.Start() thdWorkerSub.Join() Draw = Nothing End If Thread.Sleep(100) Loop End Sub End Module Public Class clsDraw Public Sub DoIt '491 lines of code that creates a new bitMap(theBmp) and draws a stock market "tick chart". 'There are two uses of Monitor.Enter > Monitor.Exit on other objects without difficulties in this code. Try lockedGraphics = Monitor.TryEnter(theFrm.bmpTicks, 50) If lockedGraphics = True Then theFrm.bmpTicks = theBmp End If If lockedGraphics = True Then Monitor.Exit(theFrm.bmpTicks) End If Thread.Sleep(10) theFrm.pbTicks.Invalidate() Catch ex As Exception Ers.Show(c_id & "theTks.Draw.DoIt-setdown2", ex) End Try End Sub End Class Monitor.Exit(theFrm.bmpTicks) throws the error. When you step through the code in the debugger, the lock is acquired and the bitmap is copied. I use Monitor to synchronize my threads in many programs that work together to track the stock market. I have not encountered this error before. Why is it occuring? Thanks. RCarey
RCarey
Whoops! Solved the problem. My code changed the object that I had a lock on. Monitor looses track of which object has the lock. Exit tries to unlock the new bitmap which does not have a lock and then throws the error. The solution is to create a seperate locking object for monitor to lock, then change the bitmap, and unlock the object. Other procedures use the locking object also. Works. Thanks RCarey
RCarey