Transfear a List from 1 thread to another safely
I Made a Program in vb.net that logs activity.
i would like thouse logenteties i make, to get saved on a mssql database.
so i make a execution each 30 sec
If lastDBupdate.AddSeconds(30) < DateTime.Now Then
Dim sql As DatabaseTool = New DatabaseTool()
lastDBupdate = DateTime.Now
End If
DatabaseTool
Imports System.Data.SqlClient
Public Class DatabaseTool
Public Sub New()
Dim sqlt As Threading.Thread = New Threading.Thread(AddressOf
SaveCompiledData)
sqlt.Start()
End Sub
Public Sub SaveCompiledData()
Try
Dim connString As String = "server=pancakes\tastegood;
database=wonttellyou;" + _
"uid=thisuserwillnotwork;
pwd=thispasswordischanged"
Dim conn As New SqlConnection(connString)
Dim cmdString As String = "INSERT INTO CallTable" + _
"([callUID],[call_id],[call_direction],[calling_party_number],[dialed_party_number],[account_code],[start_time],[end_time],[system_id],[caller_id])"
+ _
"VALUES(NEWID(),1,1,'888','888','MBA','" & DateTime.Now &
"','" & DateTime.Now & "',1,'450')"
Dim cmd As New SqlCommand(cmdString, conn)
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
problem is, i need to buffer the data before each interval, and the
DatabaseTool is running on its own Thread. so i need to make the buffer
thread safe, and copy it to the DatabaseTool thread.
will synclock be able to Work for that? and how?
No comments:
Post a Comment