Wrox Home  
Search P2P Archive for: Go

  Return to Index  

ado_dotnet thread: Multithreading and Sqlconnection using VB.NET


Message #1 by "Ruth Yakubu" <ruyakubu@h...> on Tue, 3 Sep 2002 03:02:05
Hi,

I have a start button that starts two threads.  One tread opens a sql 
connection to machine A and runs 80 different queries on a DB.  The other 
thread open a sql connection to machine B and also run 80 different 
queries on a DB. In addition, the data collected from the queries are 
being written to an output directory.

I also a stop button that is suppose to stop the application at anytime.

My problem is, when I try to stop the application, I get an error message 
stating that the thread is still running.  So, I am wondering how I can 
stop the queries from running remotely and stop the thread successfully.

The following is my code:

Imports System.Data.Common
Imports System.IO
Imports System.Data.SqlClient
Imports System.Threading
Imports System.Security.Permissions
Imports System.Security.SecurityException



Public Class Form1
    Inherits System.Windows.Forms.Form

    '   Public tSmall As New Thread(AddressOf Me.startSmall)
    Public tBig As New Thread(AddressOf Me.startBig)

    Dim sCounter As Integer = 0
    Dim bCounter As Integer = 0

    Dim btxt As Object
    Dim stxt As Object

    Dim j As Integer = 0

    Dim b = CreateObject("Scripting.FileSystemObject")
    Dim outAdapter As New SqlDataAdapter()

    Public Shared drReaderA As SqlDataReader
    Public Shared drReaderB As SqlDataReader

    Dim s_q1 As Integer
    Dim b_q1 As Integer

    '   Dim estimated_smallTime() As Double = {32.01, 52.01, 29.02, 24.5}
    Dim estimated_bigTime() As Double = {3.0, 3.1, 9.54, 6.78}
    Dim queryArray() As Integer = {6, 2, 20, 22}

    Dim strSQL As String = "executeQuery"

    '   Dim connSmall As String 
= "Server=MachineA;database=tpch30g;uid=sa;password=;"

    Dim connBig As 
String= "Server=MachineB;database=tpch30g;uid=sa;password=;"

    '  Dim strConnSmall As New SqlClient.SqlConnection(connSmall)
    Public strConnBig As New SqlClient.SqlConnection(connBig)


    Private Sub Start_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles Button1.Click

        tBig.Start()
       ' tSmall.Start()

    End Sub


    Private Sub Stop_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles Button2.Click
        Try
            tBig.Abort()
            tSmall.Abort()

        Catch A As ThreadAbortException
            If tBig.IsAlive Then
                tBig = Nothing
            End If

           If tSmall.IsAlive Then
                tBig = Nothing
            End If
            drReaderA.Close()
            drReaderB.Close()
            strConnBig.Close()
            strConnSmall.Close()

        End Try

    End Sub

    Sub startBig()

        Dim startTime As Double
        Dim endTime As Double
        Dim elpsTime As Double
        Dim streamCounter As Integer = 0

        Do Until streamCounter = 20
            For j = bCounter To 3

                Dim commSQL As New SqlClient.SqlCommand()
                commSQL.Connection = strConnBig
                commSQL.CommandType = CommandType.StoredProcedure
                commSQL.CommandText = "executeQuery" & queryArray(bCounter)
                commSQL.CommandTimeout = 90000

                If strConnBig.State = ConnectionState.Closed Then
                    strConnBig.Open()
                End If

                startTime = Timer
                btxt = b.OpenTextFile("c:\bigdata.txt", 8, True)
                btxt.WriteLine("S," & startTime)

                'outAdapter.SelectCommand = commSQL
                'Dim outData As New DataSet()
                'outAdapter.Fill(outData)

                drReaderA = commSQL.ExecuteReader()

                endTime = Timer
                elpsTime = endTime - startTime

                btxt.WriteLine("E," & elpsTime)
                btxt.Close()
                drReaderA.Close()
                bCounter = bCounter + 1
            Next j

            streamCounter = streamCounter + 1
            bCounter = 0
        Loop
        drReaderA.Close()
        strConnBig.Close()

    End Sub

 Sub startBig()

        Dim startTime As Double
        Dim endTime As Double
        Dim elpsTime As Double
        Dim streamCounter As Integer = 0

        Do Until streamCounter = 20
            For j = sCounter To 3

                Dim commSQL As New SqlClient.SqlCommand()
                commSQL.Connection = strConnSmall
                commSQL.CommandType = CommandType.StoredProcedure
                commSQL.CommandText = "executeQuery" & queryArray(sCounter)
                commSQL.CommandTimeout = 90000

                If strConnSmall.State = ConnectionState.Closed Then
                    strConnSmall.Open()
                End If

                startTime = Timer
                stxt = b.OpenTextFile("c:\smalldata.txt", 8, True)
                stxt.WriteLine("S," & startTime)

                'outAdapter.SelectCommand = commSQL
                'Dim outData As New DataSet()
                'outAdapter.Fill(outData)

                drReaderB = commSQL.ExecuteReader()

                endTime = Timer
                elpsTime = endTime - startTime

                stxt.WriteLine("E," & elpsTime)
                stxt.Close()
                drReaderB.Close()
                sCounter = sCounter + 1
            Next j

            streamCounter = streamCounter + 1
            bCounter = 0
        Loop
        drReaderB.Close()
        strConnSmall.Close()

    End Sub


End Class




  Return to Index