Solved

VB.Net Multithreading Assistance

Posted on 2014-09-25
2
181 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 13

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The Fluent Interface Design Pattern You can use the Fluent Interface (http://en.wikipedia.org/wiki/Fluent_interface) design pattern to make your PHP code easier to read and maintain.  "Fluent Interface" is an object-oriented design pattern that r…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

13 Experts available now in Live!

Get 1:1 Help Now