Solved

how to close/exit threads correctly

Posted on 2006-11-12
10
174 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
Comment Utility
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
Comment Utility
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
Comment Utility
Can you please post the code around the timeoutthread.abort() line?
0
 

Author Comment

by:Johnny
Comment Utility
   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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Johnny
Comment Utility
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
Comment Utility
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
Comment Utility
   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
Comment Utility
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
Comment Utility
please give me till afer thanksgiving...im going away fro the weekend no time to check this sorry
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now