Solved

Gracefully force my application to close

Posted on 2009-07-12
6
433 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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