Re: Threading.Monitor.Enter that doesn't /quite/ block the thread
- From: Jon Brunson <jon.brunson@innovation-NOSPAM-software-DOT-co-DOT-uk>
- Date: Thu, 06 Apr 2006 16:02:02 +0100
Jon Brunson wrote:
Daniel Moth wrote:
If you can post a small sample demonstrating the issue/need then we can discuss it over something concrete.
' Assume MyData.sdf exists, and contains a table called "SomeTable",
' which has two columns: ID int AUTO_INCREMENT,
' and Description nvarchar(255)
Private Shared __Connection As SqlCeConnection
Friend Shared Property _Connection() As SqlCeConnection
Get
If __Connection Is Nothing Then _
__Connection = New SqlCeConnection
Return __Connection
End Get
Set(ByVal Value As SqlCeConnection)
If Value Is Nothing Then _
Value = New SqlCeConnection
__Connection = Value
End Set
End Property
Public ReadOnly Property Connection() As SqlCeConnection
Get
Try
Threading.Monitor.Enter(_Connection)
If _Connection.ConnectionString Is Nothing OrElse _Connection.ConnectionString.Trim().Length = 0 Then
_Connection.ConnectionString = "Data Source = 'MyData.sdf'"
End If
If _Connection.State <> ConnectionState.Open Then
_Connection.Open()
End If
Return _Connection
Catch
Throw
Finally
Threading.Monitor.Exit(_Connection)
End Try
End Get
End Property
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
Try
Me.ListBox1.DataSource = GetSomeData()
Me.ListBox1.ValueMember = "ID"
Me.ListBox1.DisplayMember = "Description"
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Function GetSomeData() As DataTable
Dim da As SqlCeDataAdapter
Try
Threading.Monitor.Enter(_Connection)
da = New SqlCeDataAdapter("SELECT ID, Description FROM SomeTable", Connection)
Dim rtn As New DataTable
da.Fill(rtn)
Return rtn
Catch
Throw
Finally
If Not (da Is Nothing) Then da.Dispose()
Threading.Monitor.Exit(_Connection)
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As New Threading.Thread(AddressOf SyncNow)
t.Start()
End Sub
Private Const MaxValue As Integer = 100000
Private CurrentValue As Integer
Public Sub SyncNow()
Dim cmd As SqlCeCommand
Try
Threading.Monitor.Enter(_Connection)
cmd = New SqlCeCommand("INSERT INTO SomeTable (Description) VALUES(?)", Connection)
cmd.Parameters.Add("@Description", SqlDbType.NVarChar)
For i As Integer = 1 To MaxValue
cmd.Parameters(0).Value = i
cmd.ExecuteNonQuery()
CurrentValue = i
Me.Invoke(New EventHandler(AddressOf UpdateProgressBar))
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Threading.Monitor.Exit(_Connection)
If Not (cmd Is Nothing) Then cmd.Dispose()
End Try
End Sub
Public Sub UpdateProgressBar(ByVal sender As Object, ByVal e As EventArgs)
Me.barTotal.Value = CInt((CurrentValue / MaxValue) * 100)
End Sub
Full code available at: http://www.tfxsoft.com/vitani/Form1.vb
.
- Follow-Ups:
- Re: Threading.Monitor.Enter that doesn't /quite/ block the thread
- From: Daniel Moth
- Re: Threading.Monitor.Enter that doesn't /quite/ block the thread
- References:
- Threading.Monitor.Enter that doesn't /quite/ block the thread
- From: Jon Brunson
- Re: Threading.Monitor.Enter that doesn't /quite/ block the thread
- From: Daniel Moth
- Re: Threading.Monitor.Enter that doesn't /quite/ block the thread
- From: Jon Brunson
- Threading.Monitor.Enter that doesn't /quite/ block the thread
- Prev by Date: Verify Web Service Server exists...
- Next by Date: ProcessEntry.GetProcesses() throwing exception
- Previous by thread: Re: Threading.Monitor.Enter that doesn't /quite/ block the thread
- Next by thread: Re: Threading.Monitor.Enter that doesn't /quite/ block the thread
- Index(es):
Loading