Solved

Gracefully force my application to close

Posted on 2009-07-12
6
429 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
Comment Utility
And the problem is ?
0
 

Author Comment

by:peispud
Comment Utility
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
Comment Utility
Oops... My mistake

If I click on button1 first,  then I must click on button2 twice to exit the app.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

9 Experts available now in Live!

Get 1:1 Help Now