Solved

how to close/exit threads correctly

Posted on 2006-11-12
10
219 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:Johnny
[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
  • 5
  • 5
10 Comments
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17928028
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
 

Author Comment

by:Johnny
ID: 17928062
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
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17928200
Can you please post the code around the timeoutthread.abort() line?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Johnny
ID: 17930459
   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
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17934955
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
 

Author Comment

by:Johnny
ID: 17935228
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
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17935330
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
 

Author Comment

by:Johnny
ID: 17955904
   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
 
LVL 16

Accepted Solution

by:
Hillwaaa earned 500 total points
ID: 17962393
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
 

Author Comment

by:Johnny
ID: 17989349
please give me till afer thanksgiving...im going away fro the weekend no time to check this sorry
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

688 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