Link to home
Start Free TrialLog in
Avatar of garyinmiami2003
garyinmiami2003Flag for United States of America

asked on

Me.Close() Problem

I have a VS VB.Net Windows App.  The app has 3 forms.  It is called by another Application.
The user has to hit the Exit button twice to make it close.  I have tried several things:

I know that the button click event is firing and it get to a statement Me.Close().  It executs the butnClick event and returns control to the form.  User clicks again and it closes ok.

The question is how to get rid of the 2nd Exit??
I have tried Me.Dispose, Application.Close.  I even have a sleep loop to try the me.close() 5 times every few seconds.
None of it has worked.
 
 
ASKER CERTIFIED SOLUTION
Avatar of ghayasurrehman
ghayasurrehman
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Agreed Idle Mind
Avatar of garyinmiami2003

ASKER

ghayasurrehman:

 Sorry.  I did use Application.Exit()  not Application.Close().

Close the form or the App?

I need to Close the App.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
silemone: & Idle Mind:

The Startup object is "frmMain"
The Exit Button is on "frmMain"

How are the forms being shown.  The main form is on the load event.  The others are show Dialog.

Are there any infinite loops?  I don't think so but I will not be able to quickly rule it out.

I will show you code but here is a narative first:
A very large third party app calls a Class Library.  The class Library evaluates data it gets from the app and determines which windows exe to call.
A specific Windows executable is a problem.
The windows app closes returning control to class library which returns a "True" to the third party app and closes.
The problem lies in the Windows App.  It goes to exit button handle sub and appears to return control back to the form where if the exit button his hit again, it closes.
what code is the close code in, is it in the main form? you mentioned 3 forms, has it been setup so one form calls another within that app?
ok, ignore my last post since you answered it at the same time!
attach your project and send here, I will fix it for you
rockiroads:

Initially, there was only the me.Close()

Then I started experimenting trying to fix the problem. I do not see how I can put breakpoints in it.  It is anything but standalone.

I did put some message Boxes in it and know it is getting to the ExitButton Click handle.  It acts like "Me" is not the entire application until the 2nd time thru.  
ghayasurrehman:

There is nothing I'd rather do than sending it to you.  Problem for me is that it would probably lead to my unemployment.  So let's see what we can do first and maybe revisit that in a little while.
What about issuing an ExitThread followed by Exit

Application.ExitThread
Application.Exit

Application.Exit is supposed to close all
ExitThread is supposed to close form that sub app

Dont think it will make a difference but without knowing the app,, its clutching at straws lol
have you tried?
System.Diagnostics.Process.GetCurrentProcess().Kill()
2nd exit shouldnt be called actually
what about hiding the form then quitting ? probably wont make much difference if close doesnt do anything but perhaps worth a try

when you say the other forms opened as dialog, are these modal?
do you keep the handle on the open forms? perhaps close them as part of this exit button click
There is another way to close an app - Environment.Exit(0)
Do you have any code in your main form's FormClosing or FormClosed event to prevent it from closing the first time?

Everyone

I have tried everything everyone has sent so far.  None of it gives me a different outcome.  Can I make the button Click Event file automatically?  I am in the button Click handle event.  What event always fires AFTER that?

Environment.Exit(0)
        Application.ExitThread()

        Me.Close()
        Application.Exit()
System.Diagnostics.Process.GetCurrentProcess().Kill()

Zhaolai:
Do you have any code in your main form's FormClosing or FormClosed event to prevent it from closing the first time?

Those events are not used
please see if there is a ghost in your APP lol
I would tend to think it has more to do with the way in which the apps are launched and who is the "parent" of who...

...sounds kinda complicated in your "narrative" back here:
https://www.experts-exchange.com/questions/26462772/Me-Close-Problem.html#33640576

Can you explain more about how the different forms/apps interact and possibly show some code?
Is there an Event that ALWAYS fires after The button_Click event that I could possibly cause the Button_Click event to fire without user involvement??
Do you have anything in the MouseUp() or MouseLeave() events of the Button?
Try this:

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
        Static flag As Boolean
        If flag = False Then
            flag = True
            btnExit.PerformClick()
        End If
    End Sub

Open in new window

No, but...

User Clicks the button and he/she thinks it is the end.  I could move the first Close off  the button_click and put the 2nd close on the Button_Click.  To try this I need two events that fire one after the other all the time.

As I see it.
To each expert who participated in this question:  
Thank you for your time.  I will need to leave shortly.  I have been and continue to keep trying every idea you come up with.  So far, I'm at trial and error or just rebuilding the app.
So what if you went through and closed any open forms ? perhaps there is something running


on your button click event

        Dim i As Integer

        For i = Application.OpenForms.Count - 1 To 0 Step -1
            If Application.OpenForms.Item(i) IsNot Me Then
                Application.OpenForms.Item(i).Close()
            End If
        Next i

        Me.Close

        Application.Exit


Reading up on Application.Exit :

CAUTION The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.


I set the program up to run standalone rather being called by another program.  The program closed normally.  No hitting exit button twice.
I am attaching code for how the program is called.  Maybe the problem can be addressed in the "Calling" program rather than the "Called"  
Code is pasted below:


 Public Function Call_ZYCOBMNT(ByVal strPath As String, ByVal strParms As String) As String

        Dim myprocess As New Process

        With myprocess.StartInfo
            .FileName = strPath & "ZYCOBMNT.exe" & " "
            .Arguments = strParms
            .ErrorDialog = False
            .UseShellExecute = False
            .RedirectStandardOutput = True
            .RedirectStandardError = True
            .RedirectStandardInput = True

        End With
        Try
            myprocess.Start()
        Catch ex As Exception

            MessageBox.Show(ex.Message)
        End Try

        Try
            myprocess.WaitForExit()
        Catch ex As Exception

        End Try

        Return myprocess.ExitCode.ToString
    End Function