Solved

vb.net multithreading try catch abort the correct thread

Posted on 2014-04-16
8
686 Views
Last Modified: 2014-04-16
Basically I have 18 threads that all run the same subroutine but are given different stored procedure names to run. This part works great, I am trying to do the catch part of the try catch and I have come to the conclusion that if a stored procedure errors I don't know how to tell the subroutine to abort the correct thread ?

so the question is how does the subroutine know the name of the thread that is calling it so that I can do some sort of "thread name".abort to stop the thread with the error.
0
Comment
Question by:Bryon_connexus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 

Author Comment

by:Bryon_connexus
ID: 40004684
here is the code:

Dim starttime As DateTime = Now
        Dim newThread As New Thread(AddressOf DoWork)
        newThread.Start("MakeAccountIndividualTable")
        Dim newThread1 As New Thread(AddressOf DoWork)
        newThread1.Start("MakeAccountTable")
        Dim newThread2 As New Thread(AddressOf DoWork)
        newThread2.Start("MakeachATMTable")
        Dim newThread3 As New Thread(AddressOf DoWork)
        newThread3.Start("MakeATMRawTogether")
        Dim newThread4 As New Thread(AddressOf DoWork)
        newThread4.Start("makebase_dtrcdTable")
        Dim newThread5 As New Thread(AddressOf DoWork)
        newThread5.Start("Makebase_ltrcdTable")
        Dim newThread6 As New Thread(AddressOf DoWork)
        newThread6.Start("MakeDraftconversionnbrTable")
        Dim newThread7 As New Thread(AddressOf DoWork)
        newThread7.Start("MakeemailTable")
        Dim newThread8 As New Thread(AddressOf DoWork)
        newThread8.Start("MakeIndividualTable")
        Dim newThread9 As New Thread(AddressOf DoWork)
        newThread9.Start("MakeMembershipTable")
        Dim newThread10 As New Thread(AddressOf DoWork)
        newThread10.Start("MakeMembershipparticipantTable")
        Dim newThread11 As New Thread(AddressOf DoWork)
        newThread11.Start("MakemembershippropertyTable")
        Dim newThread12 As New Thread(AddressOf DoWork)
        newThread12.Start("MakemembershipserviceTable")
        Dim newThread13 As New Thread(AddressOf DoWork)
        newThread13.Start("MakeMembershipsafeboxTable")
        Dim newThread14 As New Thread(AddressOf DoWork)
        newThread14.Start("MakeOwnershipcategoryTable")
        Dim newThread15 As New Thread(AddressOf DoWork)
        newThread15.Start("MakeProcessinggroupTable")
        Dim newThread16 As New Thread(AddressOf DoWork)
        newThread16.Start("MakeProductTable")
        Dim newThread17 As New Thread(AddressOf DoWork)
        newThread17.Start("MakeProductTable")


Public Shared Sub DoWork(ByVal data As Object)
        'Dim spname As String
        'Dim tname As String
        'Dim split As String() = data.Split(New [Char]() {" "c, ","c, "."c, ":"c, CChar(vbTab)})
        'spname = split(0)
        'tname = split(1)
        Dim sqlconnstring As String = My.Settings.datamartconnstring
        Console.WriteLine("Static thread procedure. Data='{0}'", data)


        Try

            Dim sqlConn As New SqlConnection(sqlconnstring)
            Dim cmd As New SqlCommand

            Dim myParm As SqlParameter = cmd.Parameters.Add("@RowCount", SqlDbType.Int)
            myParm.Direction = ParameterDirection.ReturnValue
            Dim reader As SqlDataReader

            cmd.CommandText = data
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = sqlConn
            cmd.CommandTimeout = 0
            sqlConn.Open()

            reader = cmd.ExecuteReader()
            ' Data is accessible through the DataReader object here.

            sqlConn.Close()
            If myParm.Value < 1 Then
                MsgBox("Store procedure " & data & " Returned no records")
            End If
            Dim Recordsreturned As Integer = myParm.Value

        Catch db1 As DataException
            ********** here is where I think I need to stop/abort the thread ***********
            MsgBox(db1.InnerException.Message.ToString)

        Catch ex1 As Exception
            ********** here is where I think I need to stop/abort the thread ***********
            MsgBox(ex1.InnerException.Message.ToString)

        End Try


    End Sub
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40004698
Maybe I am missing something but doesn't the thread stop when you exit your 'DoWork' subroutine.  In other words you don't need to do anything other than have an 'exit sub' inside the catch block.
0
 

Author Comment

by:Bryon_connexus
ID: 40004712
I guess I thaught that I needed to do more with thread in reference to an error being thrown.
I haven't been able to make it cause an error to test it yet but was trying to be pro-active
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40004732
Can you post the code so that we can see what you are talking about. Also is this BackgroundWorker objects or are you handling the threading yourself?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40004747
I think you are taking suitable steps with the try...catch logic, you could try to make it crash by passing a procedure name in that doesn't exist for example.
0
 

Author Comment

by:Bryon_connexus
ID: 40004770
I've requested that this question be closed as follows:

Accepted answer: 0 points for Bryon_connexus's comment #a40004684

for the following reason:

I didn't need to post this question because it is being handled inside of the subroutine.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40004771
erm - isn't that what I told you.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40004785
@Bryon_connexus, you would not have known that unless  AndyAinscow had not posted the solution.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.NET - find out if application is running on Windows 2012 server 3 55
vb.net datagrid point 4 33
Convert VB web project to C# 3 65
CSS question 16 62
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

739 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