Solved

VB.Net Multithreading Assistance

Posted on 2014-09-25
2
192 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 40

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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Software development teams often use in-memory caches to improve performance. They want to speed up access to, or reduce load on, a backing store (database, file system, etc.) by keeping some or all of the data in memory.   You should implement a …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 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