Solved

Wait for application to close VB.NET

Posted on 2010-08-30
10
482 Views
Last Modified: 2012-05-10
I am designing an application and I am in a very early stage of the design process but I am hoping to get some advise on the best way forward. What I need is a mechanism that will control the close-down of an application. E.g before the application starts of its sets a flag in the db like a "critical section flag=true". If the user tries to close the application while the code is in this critical section the application will wait until "critical section flag=false" before it closes down. Any suggestions on the best design approach for this?
0
Comment
Question by:jes12345
[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
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33557353
what kind of applciation are you talking about?console app? winforms?
0
 

Author Comment

by:jes12345
ID: 33557425
This is not decided but probably a winform running hidden in the system tray.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33557459
Does the app have any user interface?

Don't forget - the user can kill tasks with the 'task manager'.  You probably wouldn't be able to stop that.
0
Technology Partners: 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 42

Expert Comment

by:sedgwick
ID: 33557475
i'd create an abstract class which responsible for keeping the app alive if the critical flag is set to true which mean the app is still in critical processing.
the abstract class will inherit Form object (System.Windows.Forms) and the main form of every application will be of this type (the abstract class).
the abstract class will handle the OnFormClosing event and will check the status of the critical flag.
while the flag equals to true then do nothing, otherwise call the form base class OnFormClosing to complete the closing procedure of the main form and the application itself.
the critical processing will be taken place in other thread which upon compete it will set the critical flag.
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 125 total points
ID: 33557507
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 33557690
handle the Main Form closing event and populate with your code:

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    'handle your routine to test here
End Sub

Open in new window

0
 

Author Comment

by:jes12345
ID: 33557912
I have believe I have sketched out a possible solution. Any comments?
Public Class Form1

    Friend WithEvents BackgroundWorker1 As System.ComponentModel.BackgroundWorker

    Private Sub form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        While True
            If System.IO.File.Exists("EXIT_OK.txt") Then
                System.Environment.Exit(0)
            End If
        End While

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        My.Computer.FileSystem.DeleteFile("EXIT_OK.txt")
        Me.BackgroundWorker1 = New System.ComponentModel.BackgroundWorker
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        For i As Integer = 0 To 500
            Debug.WriteLine(CStr(i))
            System.Threading.Thread.Sleep(10)

            If i = 500 Then
                My.Computer.FileSystem.WriteAllText("EXIT_OK.txt", "", False)
            End If
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BackgroundWorker1.RunWorkerAsync()
    End Sub
End Class

Open in new window

0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 125 total points
ID: 33557967
Try to kill it with the task manager - does that get around your method?
0
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 125 total points
ID: 33558175
I believe you have to hold the "Form Closing" position till you are thru. Something like firing off a BGWorker from there would do the job.

As it stands, I believe your app will exit notwithstanding.
0
 
LVL 11

Assisted Solution

by:ladarling
ladarling earned 125 total points
ID: 33562189
That wouldnt work....
You have to set the cancel flag of the FormClosingEventArgs object like:
e.Cancel = True
And instead of testing for a file system object or another flag (especially one thats generated out of the control of the UI Thread)  you could more easily test for the workers .IsBusy property and then handle the backgroundworker.RunWorkerCompleted event.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month5 days, 11 hours left to enroll

626 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