The best way to END a thread?

JasonD

Member
Joined
Aug 13, 2006
Messages
17
Location
Papillion, NE
Programming Experience
3-5
I use threads a lot in my program, and some people get that CheckForIllegalCrossThread error when loading a form.

This is the way I do my threads at the moment:

Initialization
-
VB.NET:
    Public Sub initLoad()

        Dim ThreadX As New Thread(AddressOf LoadAccount)

        ThreadX.Start()

    End Sub

Then at the end of the sub (In this example, 'LoadAccount') I have:
VB.NET:
        Thread.CurrentThread.Abort()

I believe there could be a better way to handle my threads, can you give me a heads up? I have over 6 init's (initializations) in my sources, and new threads are created a lot (Queries).

Thanks :)
 
Although I have never been taught threading in .NET, my experience of threading basics comes from Java, where the principles should be the same.

The correct way to end the thread to allow the method to expire on it's own. So in your case, when the LoadAccount method ends, the thread will end as well.

mafro
 
Ya, Ive been getting this error now in Debug mode, I haven't a clue how to fix it, if you could give me a head up that'd be great :p

The Error:

Crss.png


The code
VB.NET:
#Region "Public Variables"
    Public charname As String
    Public User As String = Form1.Temp_User
    Public Pass As String = Form1.Temp_Pass
    Public Host As String = Form1.Temp_Host
    Public Database As String = Form1.Temp_Database
#End Region

    Public Sub PlayerList()
        Try
            Dim P_List As List(Of String) = Nothing
            Dim SQL As New Odbc.OdbcConnection
            Dim Query As New Odbc.OdbcCommand
            Dim Result As Odbc.OdbcDataReader

            SQL.ConnectionString = _
            "Driver={MySql ODBC 3.51 Driver};Server=" & Host & ";Option=16834;Port=3306;Stmt=;DataBase=" & Database & ";Uid=" & User & ";Pwd=" & Pass & ";"
            SQL.Open()

            Query.CommandText = "SELECT * FROM `players`"
            Query.Connection = SQL


            Result = Query.ExecuteReader()

            Dim NAME As String = Result.GetOrdinal("Name").ToString

            While Result.Read
                lstPlayers.Items.Add(Result.GetString(NAME))
                P_List.Add(Result.GetString(NAME))
            End While

            SearchBox1.ListBoxList = P_List

            SQL.Dispose()
            SQL.Close()

        Catch Ex As Exception
            MessageBox.Show(Ex.Message & " - " & Ex.Source, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
        End Try
        lblTotal.Text = lstPlayers.Items.Count.ToString
        'ThreadX.Abort()
    End Sub

    Private Sub lstPlayers_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstPlayers.DoubleClick
        If Len(lstPlayers.Text) > 0 Then
            Dim F As New Character
            F.MdiParent = Form1
            F.charname = lstPlayers.Text
            F.initInfo()
            F.cmdDelete.Enabled = True
            F.cmdReload.Enabled = True
            F.cmdSave.Enabled = True
            F.Show()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ThreadX As Thread = New Thread(AddressOf PlayerList)
        ThreadX.Start()
    End Sub
 
You generally cannot access a control from a thread that did not create its window handle. There's an example of using delegation to access a control from a worker thread here.
 
Back
Top