Threading Problem

Phreak

Regular
Joined
Jun 7, 2002
Messages
62
Location
Iowa, United States
I'm not completely sure on how to use threads, but I followed a small fairly simple tutorial and what I used and how I used it is below.

PROBLEM: Sometimes the dataset in the FillDataSet() procedure is filled (usually after you run it a second time, usually doesn't ever work the 1st time), and sometimes it doesn't. But I receive NO error of any kind. Maybe I'm not using threads correctly. Could anyone give me some input or a better way to use threads?

[These two procedures are taken out of the main form.]

Sorry if it's a little long.

Visual Basic:
    Private Sub OracleConnect()
        Dim dbInfo As New loginInfo()
        Dim frmProgress As New progress()

        t = New Thread(AddressOf Me.FillDataSet)


        If frmLogin.DialogResult = DialogResult.OK Or cmdRefresh = 1 Then

            'run the next couple of IFs to handle someone pushing the REFRESH
            'button if they aren't logged in
            If frmLogin.txtUser.Text = "" Or frmLogin.txtPwd.Text = "" Or frmLogin.txtServer.Text = "" Then
                cmdRefresh = MsgBox("Unable to refresh tables because you are not currently logged on." & _
                             vbCrLf & "Would you like to logon now?", MsgBoxStyle.YesNo, "Refresh Problem")
            End If

            If cmdRefresh = 6 Then
                cmdRefresh = 0
                frmLogin.ShowDialog()
                OracleConnectString(frmLogin.txtUser.Text, frmLogin.txtPwd.Text, frmLogin.txtServer.Text)
                OracleConnect()
                Exit Sub
            ElseIf cmdRefresh = 7 Then
                cmdRefresh = 0
                Exit Sub
            End If

            frmProgress.Timer1.Start()
            t.Start()

            frmProgress.ShowDialog()
            frmProgress.Timer1.Stop()

            'clear combobox items
            cmbTables.Items.Clear()
            'cmbTables.Items.Add("--None Selected--")

            'clear datagrid
            dsSeedList.Clear()
            dsSeedList.Reset()
            DataSet21.Clear()
            DataGrid1.Refresh()
            OleDbConnection1.Close()

            frmProgress.Timer1.Start()
            t.Start()

            frmProgress.ShowDialog()
            frmProgress.Timer1.Stop()

            intTblCnt = DataSet21.USER_TABLES.Rows.Count
            cmbTables.DropDownStyle = ComboBoxStyle.DropDownList

            'put avail tables into listbox (combobox)
            For i = 0 To intTblCnt - 1 Step 1
                cmbTables.Items.Add(DataSet21.USER_TABLES.Rows.Item(i).Item(0))
            Next

            If cmbTables.Items.Count > 0 Then
                cmbTables.SelectedItem = cmbTables.Items.Item(0)
            End If

            If connError = 1 Then
                connError = 0
                connError = CType(MsgBox("There was an error while trying to connect to the database." & _
                            "  Would you like to try again?", MsgBoxStyle.YesNo, "Retry Connection?"), Short)
                If connError <> 6 Then
                    connError = 0
                Else
                    frmLogin.ShowDialog()
                    If frmLogin.DialogResult = DialogResult.Cancel Then
                        Exit Sub
                    Else
                        With dbInfo
                            .user = frmLogin.txtUser.Text
                            .pass = frmLogin.txtPwd.Text
                            .serv = frmLogin.txtServer.Text
                        End With

                        Call OracleConnectString(dbInfo.user, dbInfo.pass, dbInfo.serv)
                        Call OracleConnect()
                        ButtonPressed = False
                    End If
                End If
            End If
        End If
        cmdRefresh = 0
        connError = 0
        t.Abort()
        Call CheckForTables()
    End Sub
#End Region

#Region "Fill Table Listing"
    Private Sub FillDataSet()

        Try

            'fill the dataset with the list of tables
            OleDbDataAdapter2.Fill(DataSet21, "USER_TABLES")

        Catch ex As System.Exception

            'in case an exception is thrown, give an error box and
            'a moderate description of where the exception was thrown
            If ex.GetType.ToString = "System.Threading.ThreadAbortException" Then
                'do nothing... just ignore it.  it's dumb and has no idea what it's doing.
            Else
                MsgBox("Error retrieving tables!" & vbCrLf & "Details:" & vbCrLf & "    " & _
                        ex.GetType.ToString & vbCrLf & vbCrLf & "Check your username and password " & _
                        "and make sure they are correct.", MsgBoxStyle.Critical, "Table Retrieval Error")
                connError = 1
            End If
        Finally
            OleDbConnection1.Close()
        End Try
    End Sub
#End Region
 
The way you have it now, it is stateless, you aren't getting any feedback on the thread. You will need to check on the ThreadState.
And the t.Abort() is miss-placed.
 
What do you mean ThreadState? What state am I looking for? And if the t.Abort() is misplaced, where should it go? Like I said, I'm not familiar with using threads at all. I just need to use them in this particular occassion because of the time it takes to fill the dataset. Thanks for the reply.
 
I have only worked on Stateless (asynchronous) threading, but as a guess I would try something like this...
'this is untested ( let me know if it works)
Visual Basic:
frmProgress.ShowDialog() 
Do While t.ThreadState = ThreadState.Running

Loop
frmProgress.Timer1.Stop()
and t.Abort() should be used to abort the thread if there is an exception
 
Back
Top