VB.Net Multithreading Assistance

I'm new to Multithreading I was able to get it working in a simple two thread example by using this code:

    Dim t1 As New Threading.Thread(AddressOf subUDPull)
    Dim t2 As New Threading.Thread(AddressOf subSusPull)

    If Trim(txtUserID.Text.ToString) = "" And Trim(txtFirstNm.Text.ToString) = "" And Trim(txtLastNm.Text.ToString) = "" Then
      badlist = ""
      Try
        t1.IsBackground = True
        t1.Start()
      Catch ex As Exception
        MyMsg = "Error on 'MT1 Pull User Details': " & vbCrLf & ex.Message
        DisplayErr(MyMsg)
      End Try

      Try
        t2.IsBackground = True
        t2.Start()
      Catch ex As Exception
        MyMsg = "Error on 'MT1 Pull User Details': " & vbCrLf & ex.Message
        DisplayErr(MyMsg)
      End Try
    End If

    Do While t1.ThreadState = ThreadState.Running Or t2.ThreadState = ThreadState.Running
      Thread.Sleep(10)
    Loop

Open in new window


The above was a simple proof of concept implementation, while it's likely not the most efficient implementation.  (First attempt for me)

The following is going to be much more complicated, I have about 10-15 threads to execute in parallel.  

    SelStmt = "Comment on USER " & Trim(UCase(ThisUser)) & " is '" & Trim(myCommStr) & "';"
    MyExeVal = EXENonQry(SelStmt, "Comment on USER")
    funcErrorPL(MyMsg, MyExeVal)

    SelStmt = "revoke all on " & Trim(UCase(ThisUser)) & " from " & Trim(UCase(ThisUser)) & ";"
    MyExeVal = EXENonQry(SelStmt, "Revoke all")
    funcErrorPL(MyMsg, MyExeVal)

Open in new window


Here are the issues I'm having...
1) The function that I'm using  to execute the SQL statements (EXENonQry) returns a string (completion result) and requires two parms. The above example I can only pass one parm (t1.start(PARM)) and am not sure if the tread can return the string from EXENonQry.
2) I want to get away from declaring each thread name but still be able to track the threads completion to be sure all threads complete before proceeding later in the sub.  I don't really care what the threads are named I just need to know what the completion result is for the processes being executed.  I think the completion tracking part requires 'synchronizing threads' but I can't seem to wrap my head around how this works...  Perhaps my brain isn't working this week.  D=

As always I appreciate any assistance you can provide, thanks for your time!!!
EDW_GideonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
For the first it's possible to pass a string array or List<string>  as the parameter.   You would then cast that once inside the function to get all the values you need.

There are a number of different ways of doing this, but what I've done in the past is create a global DataTable.  I DO recommend naming them so that you can keep track of each one.  When you instantiate the thread, create a row in the datatable, set some intitial value (say "-1") to the output.  When the thread completes, the thread needs to LOCK the datatable first, update the row based upon the name, and the release the lock.  

This makes the DataTable thread safe,   You can then read this in some kind of while in the parent thread, and continue on when everything is done.

Note you only need to lock on a write . . . not a read.
0
frankhelkCommented:
Some other attempts for passing more than one parameter:

Quick & dirty:
Stuff all the parameters into one string, separated by a delimiter character. Once the string is passed, breakt it up again with Split(). Good if you'll like to pass a (possibly variable) number of strings.

Somewhat less dirty, but tricky
Stuff all the parameters into a Collection object. Pass that object and retrieve the parameters out of it. Needs some work to extract the parameters correct, but you could pass anything you'll like. Very flexible.

Fine art:
Define a class that contains the parameters as properties. Instantiate an object, set the properties and pass that object as parameter. That way you'll be able to pass anything encapsulated to the thread ... strings, numbers, booleans, or any type of objects of your choice, and even arrays of 'em (which involves flexibility, too).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.