Solved

Creating multithreads against the same class and joining them together after. vb.net

Posted on 2013-11-27
5
298 Views
Last Modified: 2013-12-01
I have searched as long as my eyes can take it and can't find quite the solution I need.  I'm creating an application that will run multiple SQL queries against a data set and rather than wait for each to finish in turn, I would like to kick off 2 at a time (using different threads for each).  The following code seems to work to create one additional thread that works, but anything beyond that seems to get destroyed (or doesn't work).

'First I create a data reader that determines how many total SQL queries need to be run
'I then loop through them...and I'm TRYING (failing) to run 2 concurrently...then waiting
'for them to finish...then moving on to the next 2

'Inside the RUNSQL class, I'm just running a simple statement and getting one value back...

'What is really strange is that if I run this in debug mode and step through it, it works...so
'the code must be stepping on itself in "real time"

'In the real code I'm trying to run 5 threads, but I've cut it back for brevity sake

If oRST.HasRows Then
            Dim intCounter As Integer = 0
            Dim oThread(1) As System.Threading.Thread
            Dim oRunSQL As New RunSQL
            Do While 1 = 1
                oRST.Read()
                intFTPKey = oRST.Item("ftp_key")
                If Err.Number <> 0 Then Exit Do
                strSQL = oRST.Item("ifs_sql").ToString
                oThread(intCounter) = New Thread(AddressOf oRunSQL.Runner)
                oRunSQL.vSQL = strSQL 'This is where I pass the unique SQL statement to the new thread
                oThread(intCounter).IsBackground = True
                oThread(intCounter).Start()
                intCounter = intCounter + 1
                If intCounter = 1 Then
                    intCounter = 0
                    oThread(0).Join()
                    oThread(1).Join()
 
                End If
            Loop
            oThread(0).Join()
            oThread(1).Join()
        End If

Anyone know what the heck I'm missing here???
0
Comment
Question by:ryanmaves
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:ShazbotOK
ID: 39682046
Your setting your int counter to 1 right away before you evaluate if the counter equals 1... therefore it will allways enter the if statement on the 1st pass.

move your incrementer to after your (if) statement so it increments 1 after the fact.
0
 

Author Comment

by:ryanmaves
ID: 39682062
You're right...but that won't solve the real issue.  I modified the code down to 2 threads...the original was checking to see if intCounter was equal to 5....(I just slimmed it down after being too tired).

When testing, I tested against a possible two threads but used code that I hoped would handle 5....so there were only two passes through the code (neither of which would have triggered the IF intCounter = 5).
0
 
LVL 33

Expert Comment

by:sarabande
ID: 39682899
Dim oThread(1) As System.Threading.Thread

Open in new window

how can you use two threads with that?

you also may post the thread code. if two threads are dealing with the same database, you should have a separate connection for each (1 environment) or the threads would mix-up their statements and results.

Sara
0
 

Author Comment

by:ryanmaves
ID: 39682906
I can post the thread code, but in short...the code is in a class and within the worker sub the connection is declared, started, and closed.  I thought that doing so would essentially create a new connection for each thread.  If that's wrong, then I suppose I can't use it.
0
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 39682927
...the code is in a class and within the worker sub the connection is declared, started, and closed.  I thought that doing so would essentially create a new connection for each thread.

it sounds as it would be ok. when dealing with a database you have three resources: the resources for the environment, for the connection and for the individual statement to execute.

the environment handles things like name of the data source, access rights, and number of open connections. the individual connection resources were needed to hold multiple and asynchronously running statements together. for example passing the select statement and getting the result set by one or multiple fetch operations are separate operations which only can be done using one connection exclusively. so your threads may share the environment but never the same connection. if using a new connection in each thread it should be sufficient. then you only have to care that the result sets returned by the database are also exclusive and would not go to shared data.

Sara
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 77
DataGridView does not show data rows correctly in vb.net 2013 3 29
Close tabpage of custom control 9 23
Help with LINQ and XML 10 25
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now