Threading.Monitor.Exit(x) throws ArgumentException... any ideas why?
-
Hi All! I have simple application running under Windows CE 4.2, using compact framework 1.0 SP3. There are two timers (threads) operating in the program which read/manipulate a common variable of type integer. To prevent critical section collisions I am using the Threading.Monitor.Enter / Exit methods to acquire and release a lock on the variable in question. The Enter method call works. The Exit method call throws an exception, and I don't know why. Here is a sample chunk of code.
Public Class FormMain Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) MyBase.Dispose(disposing) End Sub 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents inputTimer As System.Windows.Forms.Timer Friend WithEvents outputTimer As System.Windows.Forms.Timer Private Sub InitializeComponent() Me.inputTimer = New System.Windows.Forms.Timer Me.outputTimer = New System.Windows.Forms.Timer ' 'inputTimer ' Me.inputTimer.Enabled = True ' 'outputTimer ' Me.outputTimer.Enabled = True Me.outputTimer.Interval = 500 Me.Text = "Threads" End Sub Public Shared Sub Main() Application.Run(New FormMain) End Sub #End Region Private _inputs As Integer = 0 Private Sub inputTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles inputTimer.Tick Try Threading.Monitor.Enter(_inputs) _inputs += 1 Threading.Monitor.Exit(_inputs) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub outputTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles outputTimer.Tick Try Threading.Monitor.Enter(_inputs) _inputs = 0 Threading.Monitor.Exit(_inputs) Catch ex As Exception MsgBox(ex.Message)