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
Solved

Gracefully force my application to close

Posted on 2009-07-12
6
431 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:peispud
  • 3
  • 3
6 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 24836085
And the problem is ?
0
 

Author Comment

by:peispud
ID: 24836093
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.
0
 

Author Comment

by:peispud
ID: 24836098
Oops... My mistake

If I click on button1 first,  then I must click on button2 twice to exit the app.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 24836112
Well, normally your application doesn't run in a loop like that, and if so, you should use a thread or a backgroundworker for it. Using that code you don't do two click to finish, but one to select another control (the button 2) and 1 click to close the form.
You should consider two things:
1 - Use this in the loop "If Me.IsDisposed Then Exit Do", or else it will keep run event if it closes the form.
2 - Use "Application.Exit()" to close the application
0
 

Author Comment

by:peispud
ID: 24836288
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!!!
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24836299
Glad I could help, and you should explore a little about threads or BackGroundWorker (that is an easy way to handle threads)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

790 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