how to close/exit threads correctly

i have a thread application thats giving me an error of this thread 2345 is not part of 4321 and cant be closed(from another thread)

something along those lines it says...
what im looking for is to stop the program and all the threads correctly

            timeoutthread.Abort()
            timeoutthread.Join()
            timeoutthread = Nothing
its having a problem with the abort line

thank you
JohnnyAsked:
Who is Participating?
 
HillwaaaCommented:
Hi Pern,

I've whipped up a windows form to (hopefully) demonstrate how you can do this with your code.  The form does nothing when it loads, but when you click on the "start thread" button it will start up a thread call utthread (just like what you have).  this just pops up a messagebox every 5 seconds.  Then when you close the form, the closing script is run - also just like what you have.  Hopefully from this you can see how to combine it into one form - give it a shot and let me know if you have any questions!

-----------------------------------------------------------------------------------------------------------

Imports System.Threading

Public Class Form2
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(96, 48)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Start Thread"
        '
        'Form2
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form2"
        Me.Text = "Form2"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim utthread As Thread
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            utthread = New Thread(AddressOf IncUT)
            utthread.Name = "ut_Thread"
            utthread.Start()

        Catch
            'error while starting bg threads
        End Try
    End Sub

    Private Sub IncUT()
        Do
            MessageBox.Show("here")
            Thread.Sleep(5000)
        Loop
    End Sub


    Private Sub Form2_closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Try
            utthread.Abort()
            utthread.Join()
            utthread = Nothing
        Catch

        End Try
    End Sub
End Class
0
 
HillwaaaCommented:
Hi Pern,

How are you creating these threads?
Try confirming that you are trying to close a thread that you created, and that you are trying to close it from the thread that created it.

Cheers!
0
 
JohnnyAuthor Commented:
i dont understand how to use threads...(the part im trying to do was not writen by me)

so i dont know how to check...

all im looking to do is close the program without an error and all the threads is there a way to close them all

thanks
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
HillwaaaCommented:
Can you please post the code around the timeoutthread.abort() line?
0
 
JohnnyAuthor Commented:
   Public Sub close()
        Try

            utthread.Abort()
            utthread.Join()
            utthread = Nothing

            timeoutthread.Abort()
            timeoutthread.Join()
            timeoutthread = Nothing

#If Not PureLeafServer Then
            connectthread.Abort()
            connectthread.Join()
            connectthread = Nothing
#End If
        Catch ex As Exception
        End Try
    End Sub
actually im having problems with utthread.Abort() for the error...
0
 
HillwaaaCommented:
ok - is the "close()" method you posted attached to the same object (I'm guessing a windows form) that creates utthread?  Or are these threads created somewhere else?
0
 
JohnnyAuthor Commented:
this was a counsole app that was converted toa  windows form..the threads are for an irc and still in counsole code....

to answer i THINK its done someplace else
0
 
HillwaaaCommented:
Ok - in that case, what I would do is find the utthread.start() method, which should be in the setup somewhere.  Then add a new method like closeThreads to that object, and put all the above code into that method.  Then from the close() method above, call closeThreads().

That way, the threads will be closed by the same thread that created them (in theory).

Good luck!
0
 
JohnnyAuthor Commented:
   Public Function init() As Integer
        Try
            utthread = New Thread(AddressOf IncUT)
            utthread.Name = "ut_Thread"
            utthread.Start()
            timeoutthread = New Thread(AddressOf timeout)
            timeoutthread.Name = "timeout_thread"
            timeoutthread.Start()

#If Not PureLeafServer Then
            connectthread = New Thread(AddressOf connect)
            connectthread.Name = "connect_thread"
            connectthread.Start()
#End If

#If Services Then
            servicethread = New Thread(AddressOf services)
            servicethread.Start()
#End If

        Catch
            'error while starting bg threads
            Return -1
        End Try
    End Function

how would one go about doing that??
0
 
JohnnyAuthor Commented:
please give me till afer thanksgiving...im going away fro the weekend no time to check this sorry
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.