Link to home
Start Free TrialLog in
Avatar of slightlyoff
slightlyoff

asked on

Threads and MREs in VB.NET

I have an application that needs to read from 5 different databases to determine:
1. If any new orders exist in any of the databases
2. If orders do exists, they are copied to a single table on one of the databases, sorted and processed in the order they were placed.

To accomplish this, I created a class called MyThread, which takes several properties:
1.  A StoreID (to determine which database to use)
2.  A Connection String to connect to that database.

It Creates a Thread, executes a DoWork method and sets the MRE.

I've attached a sample of how I call the class below as well as the class itself.

I want all 5 MyThread objects to run on different threads and to wait for all the threads to be completed before running the rest of the program. (In the code I put NextFunctiontoRun() to show basically how I have it set up)

When I run my application, I get this error:
"WaitAll for multiple handles on a STA thread is not supported."

I've done some searching on this, but couldn't apply what I found.  In a nutshell, all I hope to do is create 5 instances of the class, execute the code, wait for them all to finish and proceed.

I hope this makes sense :)

Thanks for your help!!!





'Create object and execute the methods
'Store1
mtH = New MyThread
MREs.Add(mtH.MRE)
mtH.ConnString = Store1ConnectionString
mtH.StoreID = 1
mtH.Start()


'Store2
mtM = New MyThread
MREs.Add(mtM.MRE)
mtM.ConnString = Store2ConnectionString
mtM.StoreID = 2
mtM.Start()

'Store3
'Store4
'Store5

ManualResetEvent.WaitAll(MREs.ToArray)

NextFunctionToRun()


--------------------------------
'MyThread Class
Public Class MyThread
    Private T As Thread = Nothing
    Public ConnString As String
    Public theOrderCount As Integer
    Public StoreID As Integer
    Public MRE As New ManualResetEvent(False)
    Public needToReset As Boolean = False
    Public errorTxt As String = ""

    Public Sub Start()
        If IsNothing(T) Then
            T = New Thread(AddressOf DoWork)
            T.Start()
        End If
    End Sub

    Public Sub DoWork()
        Dim t As Thread = Thread.CurrentThread
        Dim theText As String
        Dim sqlConn As New SqlConnection
        Dim x As Integer
        Dim da2 As SqlDataAdapter
        Dim ds2 As DataSet
        Dim tempStoreID As Integer
        Dim sql, newSQL As String
        Dim myStoreID As Integer
        Dim tempConn As New SqlConnection
        Dim tempcmd As New SqlCommand


        sql = "SELECT orders from eachstore, blah blah"

        Try

        sqlConn.ConnectionString = ConnString
        sqlConn.Open()

        da2 = New SqlDataAdapter(sql, sqlConn)
        ds2 = New DataSet()
        da2.Fill(ds2, "table5")

        If ds2.Tables(0).Rows.Count = 0 Then
            'No Order from Store
            theText = "0"
            theOrderCount = 0
        Else
            theText = (ds2.Tables(0).Rows.Count).ToString
            theOrderCount = CInt(theText)
            tempConn.ConnectionString =TempConnectionString
            tempConn.Open()
            
	    For x = 0 To (ds2.Tables(0).Rows.Count - 1)
                tempStoreID = StoreID
                newSQL = "Insert into temptable blah, blah..."
                tempcmd = New SqlCommand(newSQL, tempConn)
                tempcmd.ExecuteNonQuery()
            Next

        End If

        sqlConn.Close()
        sqlConn.Dispose()
        da2.Dispose()
        ds2.Dispose()

        MRE.Set()
        Catch ex As Exception
            'Log Error - Mark for reset
            errorTxt = ex.Message
            needToReset = True
        End Try

    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of lludden
lludden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial