Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Gracefully force my application to close

Posted on 2009-07-12
6
Medium Priority
?
435 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 48

Accepted Solution

by:
jpaulino earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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