Link to home
Start Free TrialLog in
Avatar of peispud
peispudFlag for Canada

asked on

Gracefully force my application to close

Hi
I am using VB.Net 2005
See code below.  I am trying to make this application completely stop  (gracefully)  in one click on Button2 no matter what my application is doing.

Thank you
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ExitLoopTime As DateTime = DateTime.Now.AddSeconds(300)
        Do While DateTime.Compare(ExitLoopTime, DateTime.Now) > 0
            Application.DoEvents()
        Loop
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

Open in new window

Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

And the problem is ?
Avatar of peispud

ASKER

Hi

The problem is that a person has to click twice to force the application to exit before the loop is complete.  It's not a big deal,  but that is not my intention.  I wish to click Button2 just once and gracefully exit the application.
Avatar of peispud

ASKER

Oops... My mistake

If I click on button1 first,  then I must click on button2 twice to exit the app.
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peispud

ASKER

Of course you are right and I am happy about that.
I've only recently begun to work with threads.  I am beginning to see that they are very useful

Thanks!!!
Glad I could help, and you should explore a little about threads or BackGroundWorker (that is an easy way to handle threads)