Solved

how to close/exit threads correctly

Posted on 2006-11-12
10
193 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
update textboxes with Streamreader 21 47
Exit the loop 4 42
Need a starter for ETL protocol? 4 57
Show/Hide ASP.NET controls with JQuery best practices 3 48
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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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