?
Solved

VB.Net Multithreading Assistance

Posted on 2014-09-25
2
Medium Priority
?
209 Views
Last Modified: 2014-12-22
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!!!
0
Comment
Question by:EDW_Gideon
2 Comments
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 40344282
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
 
LVL 14

Accepted Solution

by:
frankhelk earned 2000 total points
ID: 40344419
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Suggested Courses

616 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