fishberg5051
Member
- Joined
- Jul 21, 2006
- Messages
- 7
- Programming Experience
- Beginner
Hey everyone, I have a question in regards to Threading an console app (as the title describes).
Here is my situation: I currently have an access Database that gets populated by a third party utility with a bunch of data, ID's, and hex messages that contain a bunch of data. My project consists of writing a parser that will grab those records in the access database, convert the hex to ascii (which is already done) and insert it into a sql tables.
I'm new to multi-threading and bought a book and have been working on a few things. I got as far as I can using a thread pool and have my classes written to pass an arugment into the class when the thread gets innitiated.
So going further into explination: I want to inniate one thread per record (so 500 records = 500 threads) then when it gets to the point of converting my hex value into ascii there is usually 5 to 27 different values derived from that value and I want to create a thread for each value, so potentially I will have about 2000+ threads running, but with proper management and killing them when I'm finished with them that shouldn't be a problem should it?
Can anyone give me an idea of what I can try to get this accomplished. I have posted below sections of my code. Currently the application works, and it takes 4.5 minutes to parse 500 records and insert the 5-27 records per parent record into sql. Ideally I want to do this in under a minute if at all possible. Thanks in advance, and sorry for the lengthy post.
so if anyone has any comments/ideas on what I can do to do "X" amount of inserts very fast I would appreciate it. Thanks for your patience.
Sincerely,
Fishberg
Here is my situation: I currently have an access Database that gets populated by a third party utility with a bunch of data, ID's, and hex messages that contain a bunch of data. My project consists of writing a parser that will grab those records in the access database, convert the hex to ascii (which is already done) and insert it into a sql tables.
I'm new to multi-threading and bought a book and have been working on a few things. I got as far as I can using a thread pool and have my classes written to pass an arugment into the class when the thread gets innitiated.
So going further into explination: I want to inniate one thread per record (so 500 records = 500 threads) then when it gets to the point of converting my hex value into ascii there is usually 5 to 27 different values derived from that value and I want to create a thread for each value, so potentially I will have about 2000+ threads running, but with proper management and killing them when I'm finished with them that shouldn't be a problem should it?
Can anyone give me an idea of what I can try to get this accomplished. I have posted below sections of my code. Currently the application works, and it takes 4.5 minutes to parse 500 records and insert the 5-27 records per parent record into sql. Ideally I want to do this in under a minute if at all possible. Thanks in advance, and sorry for the lengthy post.
VB.NET:
'how I'm calling my first class using a thread pool
[SIZE=2]Console.WriteLine([/SIZE][SIZE=2][COLOR=#800000]"Thread A: "[/COLOR][/SIZE][SIZE=2] & RecordCount & [/SIZE][SIZE=2][COLOR=#800000]" initiated"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]ThreadPool.QueueUserWorkItem([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] WaitCallback([/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] WorkerA.DoWork), RecordCount)[/SIZE]
'my first class
'######################
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] MessageProcessor[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] _IsCompleted [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] _ConnectionString [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]"Provider=Microsoft.Jet.OLEDB.4.0; Data Source="[/COLOR][/SIZE][SIZE=2] & strIAMSDBLocation & [/SIZE][SIZE=2][COLOR=#800000]";"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ReadOnly[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] IsCompleted() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Get[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] _IsCompleted[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Get[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] DoWork([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] state [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]_IsCompleted = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]SyncLock[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2](MessageProcessor)[/SIZE]
[SIZE=2]subProcessIAMSMessages(state)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]SyncLock[/COLOR][/SIZE]
[SIZE=2]_IsCompleted = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[COLOR=#0000ff]End Class[/COLOR]
[COLOR=#0000ff]'#######################[/COLOR]
'after building a virtual recordset I loop through it to insert my values into sql
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] intCurrentRecordCount [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] InsertThreads(objRS_Message.RecordCount) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Thread[/SIZE]
[SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] objRS_Message.EOF <> [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] StartInsert [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] InsertProcessor(objRS_Message([/SIZE][SIZE=2][COLOR=#800000]"DataPointId"[/COLOR][/SIZE][SIZE=2]).Value, objRS_Message([/SIZE][SIZE=2][COLOR=#800000]"SampleTime"[/COLOR][/SIZE][SIZE=2]).Value, objRS_Message([/SIZE][SIZE=2][COLOR=#800000]"DataPointValue"[/COLOR][/SIZE][SIZE=2]).Value)[/SIZE]
[SIZE=2]InsertThreads(intCurrentRecordCount) = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Thread([/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] StartInsert.DoWork)[/SIZE]
[SIZE=2]InsertThreads(intCurrentRecordCount).Name = [/SIZE][SIZE=2][COLOR=#800000]"Thread#"[/COLOR][/SIZE][SIZE=2] & intCurrentRecordCount[/SIZE]
[SIZE=2]Console.WriteLine([/SIZE][SIZE=2][COLOR=#800000]"Thread Insert Started for: "[/COLOR][/SIZE][SIZE=2] & intCurrentRecordCount)[/SIZE]
[SIZE=2]InsertThreads(intCurrentRecordCount).Start()[/SIZE]
[SIZE=2]InsertThreads(intCurrentRecordCount).Abort()[/SIZE]
[SIZE=2]intCurrentRecordCount = intCurrentRecordCount + 1[/SIZE]
[SIZE=2]objRS_Message.MoveNext()[/SIZE]
[SIZE=2][COLOR=#0000ff]Loop[/COLOR][/SIZE]
'my last class to do the insert
[SIZE=2][COLOR=#0000ff]'############################[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] InsertProcessor[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] _IsCompleted [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] _DataPointID [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] _SampleTime [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Date[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] _DPV [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] DataPointID [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] SampleTime [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Date[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] DPV [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2]._DataPointID = DataPointID[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2]._SampleTime = SampleTime[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2]._DPV = DPV[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ReadOnly[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] IsCompleted() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Get[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] _IsCompleted[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Get[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] DoWork([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] state [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]SyncLock[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2](InsertProcessor)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strSQL_GTW [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2]strSQL_GTW = [/SIZE][SIZE=2][COLOR=#800000]"query string removed for security reasons"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] conn [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlConnection[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cmd [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlCommand[/SIZE]
[SIZE=2]conn.ConnectionString = [/SIZE][SIZE=2][COLOR=#800000]"Removed for security reasons"[/COLOR][/SIZE]
[SIZE=2]conn.Open()[/SIZE]
[SIZE=2]cmd.Connection = conn[/SIZE]
[SIZE=2]cmd.CommandType = CommandType.Text[/SIZE]
[SIZE=2]cmd.CommandText = strSQL_GTW[/SIZE]
[SIZE=2]cmd.ExecuteNonQuery()[/SIZE]
[SIZE=2]cmd = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE]
[SIZE=2]conn.Close()[/SIZE]
[SIZE=2]conn = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]SyncLock[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
'#########################
so if anyone has any comments/ideas on what I can do to do "X" amount of inserts very fast I would appreciate it. Thanks for your patience.
Sincerely,
Fishberg