Results 1 to 8 of 8

Thread: [RESOLVED] Trying to exit the program dynamically, but the underlying process continues running

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    126

    Resolved [RESOLVED] Trying to exit the program dynamically, but the underlying process continues running

    [VS 2010 / Winforms]

    Hey guys,

    I've got a very simple project that only has 1 form. On form load, I am calling a custom function (in an external module) which checks to see if a specific condition is true (based on user's computer configuration).

    If the condition is not true, then I need to exit the program right away. So I am calling MyBase.Close() if the condition is not true.

    This successfully closes the GUI part of the program, but the underlying process is still running. And then every time I open/close the program again, it adds a new process (ie if I open and close the program 5 times, then there are 5 separate processes running).

    Is there something else I need to call in addition to MyBase.Close() in order to kill everything? I also tried adding MyBase.Dispose() before MyBase.Close(), but that didn't make any difference.

    This is my code for the form load...

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim Result As Boolean = TestFunction(Result)
    
            If Result = False Then
                MyBase.Close()
            End If
    
        End Sub
    
    End Class
    My actual code for the custom function is a bit complex, but I made a minimum test case which has the same result (the underlying process never dies)...
    Code:
    Module Test
    
        Function TestFunction(ByVal Result As Boolean)
    
            Dim MsgBx As DialogResult = MessageBox.Show("Test", "Test", MessageBoxButtons.OKCancel)
    
            If MsgBx = DialogResult.OK Then
                Return True
            Else
                Return False
            End If
    
        End Function
    
    End Module
    If you run the above code, and then click the 'Cancel' button every time the message box pops up, it closes the GUI, but the process continues running in the background.

    How can I fix this?

    Thanks!

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Trying to exit the program dynamically, but the underlying process continues runn

    Application.Exit

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Trying to exit the program dynamically, but the underlying process continues runn

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         If PromptForClose() Then
    5.             Application.Exit()
    6.         End If
    7.     End Sub
    8. End Class
    9.  
    10. Module Test
    11.  
    12.     Friend Function PromptForClose() As Boolean
    13.         Dim message As String = "Would you like to close the application?"
    14.         Dim result As DialogResult = MessageBox.Show(message,
    15.                                                      Application.ProductName,
    16.                                                      MessageBoxButtons.OKCancel,
    17.                                                      MessageBoxIcon.Question,
    18.                                                      MessageBoxDefaultButton.Button1)
    19.         Return result = DialogResult.OK
    20.     End Function
    21.  
    22. End Module

  4. #4
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: Trying to exit the program dynamically, but the underlying process continues runn

    Here's a recent thread where the issue of 'how to end an application' gets discussed some:

    http://www.vbforums.com/showthread.p...ght=abruptness
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Trying to exit the program dynamically, but the underlying process continues runn

    Quote Originally Posted by dolot View Post
    Here's a recent thread where the issue of 'how to end an application' gets discussed some:

    http://www.vbforums.com/showthread.p...ght=abruptness
    Thats not relevant at all to this thread.

  6. #6
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: Trying to exit the program dynamically, but the underlying process continues runn

    Quote Originally Posted by ident View Post
    Thats not relevant at all to this thread.
    I thought post #5 was.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    126

    Re: Trying to exit the program dynamically, but the underlying process continues runn

    Wow... so simple.

    Thanks ident!

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: [RESOLVED] Trying to exit the program dynamically, but the underlying process con

    Quote Originally Posted by BMAN645 View Post
    [VS 2010 / Winforms]... If you run the above code, and then click the 'Cancel' button every time the message box pops up, it closes the GUI, but the process continues running in the background.

    How can I fix this?

    Thanks!
    I ran this
    Code:
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If TestFunction() = Windows.Forms.DialogResult.Cancel Then
                Me.Close()
            End If
        End Sub
    End Class
    
    Module Test
        Function TestFunction() As DialogResult
            Return MessageBox.Show("Test", "Test", MessageBoxButtons.OKCancel)
        End Function
    End Module
    It seemed to work as expected.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width