How do i code stuff for when the X button is clicked?

Posted on 2005-05-12
Last Modified: 2010-04-23
I want to stop threads when the X is clicked, how would I go about doing this?
Question by:addicktz
    LVL 9

    Assisted Solution

    Hi addicktz,
     Use the form_closing event. If you have your form open in the editor, over in the properties box you will notice a yellow lightning bolt if you go through this you will see events for closing and closed just double click the closing and it will create the event code for you, then just enter your code to stop or close anything you want in here.

    LVL 85

    Accepted Solution

    To stop the threads you would need to keep references to them.  You could create an ArrayList at your form level and add your threads to it as you create them.  Then you can use the closing event as cyberdevil has suggested to iterate thru them and call the Abort method for each one.  

    Something like....

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private Threads As New ArrayList

        ' Somewhere in your code, create your threads and add them to the Threads ArrayList above...

        Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
            Dim t As System.Threading.Thread

            For Each t In Threads
                If t.IsAlive Then
                End If
        End Sub

    End Class

    LVL 9

    Expert Comment


    The best thing to do would be to call the Dispose() method, this will do a garbage collection as you don't need to keep a record of anything then.


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    745 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

    19 Experts available now in Live!

    Get 1:1 Help Now