Solved

Gracefully force my application to close

Posted on 2009-07-12
6
432 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
[X]
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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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