Me.Close on Windows 7/2008 - vb.net-2010

I'm in the process of writing a VB app that launches at user login. The issue I see is that on Windows 7/2008 when closing the last form using both Me.Close or Application.Exit both throw the error message "did the application close properly". On WindowsXP it closes without issue.
One of the basic features of the app is that when loading it checks some values and closes the app based on those values. Even in this instance I get the "did not close properly" message on win 7/2008. What could I be doing wrong?
Private Sub Dialog1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If fName = failsafeUser Then
allowLogin = vbTrue
Process.Start(My.Settings.ExplorerPath)
'Me.Close() ' TODO Check into why Me.Close() is ending poorly on 2008 servers
Application.Exit()
End If
End Sub

try using
End
to close your app. That should work. Obviously application.Exit is the better option, but End should work.
Thanks
Paul

Related

VB .NET application not exiting when all forms close

I wanted to create an application with two forms, a main one called Source and a subsidiary called Viewer. Using buttons on each form, the user could switch to the other, even if that form had previously been closed. So I set my program to Close When Last Form Closes. Moreover, I added message boxes when each form closed, just to check that the user was sure.
But now my application won't exit after I close both forms! It shows up in Task Manager, and if I run it in MSVS, the debugger never stops! When each form is closed, My.Application.Forms returns an empty collection. And if I force quit it using End/Application.Exit, the program still quits — see the MWE below. What do I do?
MWE
Create a blank VB.NET Windows Forms project. In the "Application" tab of your project's settings, choose "Close When Last Form Closes." In the designer, create a Form Source with one button.
Public Class Source
Public Sub Switch(sender As Object, e As EventArgs) Handles Button1.Click
My.Forms.Viewer.Show()
End Sub
Public Sub Free(sender As Object, e As FormClosingEventArgs) Handles Me.Closing
e.Cancel=(MsgBoxResult.Cancel=MsgBox("Are you sure?"))
End Sub
'Public Sub Test(sender As Object, e As EventArgs) Handles Me.Closed
'Debug.Assert(Not My.Application.Forms.Count))
'If the next line is uncommented, the application will close, like we want
'If Not My.Forms.Viewer.Visible Then Application.Exit()
'End Sub
End Class
Then create an identical form called Viewer.
Public Class Viewer
Public Sub Switch(sender As Object, e As EventArgs) Handles Button1.Click
My.Forms.Source.Show()
End Sub
Public Sub Free(sender As Object, e As FormClosingEventArgs) Handles Me.Closing
e.Cancel=(MsgBoxResult.Cancel=MsgBox("Are you sure?"))
End Sub
'Public Sub Test(sender As Object, e As EventArgs) Handles Me.Closed
'Debug.Assert(Not My.Application.Forms.Count))
'If the next line is uncommented, the application will close, like we want
'If Not My.Forms.Source.Visible Then Application.Exit()
'End Sub
End Class
Try closing any form last. Neither will cause the app to quit.
Don't use Form.Closing and Form.Closed
In MSDN's page on Form.Closing, it remarks:
Caution
The Closing event is obsolete in the .NET Framework version 2.0 [and above]; use the FormClosing event instead.
The events were deprecated because
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.
The MSDN documentation nowhere states this, but the reverse is true: in order for your form to signal to the VB.NET runtime that the form closed, Form.Closing and Form.Closed cannot be subscribed to.
If you replace Me.Closing with Me.FormClosing and Me.Closed with Me.FormClosed in your MWE, it works.

Auto close application

I'm new to this site and VB, so please keep that in mind.
I have a program which basically connects to our database and updates it, but what I want is after this code:
Private Sub Button1_Click(ByVal sender As system.EventArgs) Handels Button1.Click
If Retry = 0 then
End
Else
TextBox3.Textt = ""
Button1.text = "Ok"
Button1.enabled = False
Me.refresh()
System.Threading.Thread.Sleep(500)
Me.Form1_Load(Me, e)
System.Threading.Thread.Sleep(500)
Me.Refresh()
System.Windows.Form.Application.DoEvent()
End If
End Sub
End Clas
This form/App to close (can be closed before this part as it just tells me everything went well but still if you click ok will retry) so I don't have to close it every time I run this app to update and then annoyingly stay awake as it won't let me study the code nor let me change anything until this app is closed. sometime it takes so long that i even forget i have this app running.
I can provide more info if tell me what otherwise I don't know much about VB.
You can use Me.Close() it will close the form when executed.
Or Application.Exit() you can check HERE

Avoid refresh event in a WebBrowser control?

I am using the GEPluginControl in VB.NET (VS2010).
My app works fine. I am using the GEWebBrowser control (included on the GEPluginControl), and I am able to show the information in the Google Earth format.
However, I am having troubles when the GEWebBrowser control Refresh method is called. When this happens, the GEWebBrowser starts to fail (the earth disappears and I am not able to reload it again).
I am not calling the Refresh method explicitly. I think this method is called automatically in the following case: when the user close the window, I'm catching the event Form_Closing, in order to ask the user if really wants to exit. If the answer is No, the user will stay on the app, but the GEWebBrowser control appears blank!. I can hear the sound associated to the refresh method, so I think this method may be my trouble.
Here is the Form_Closing event code:
Private Sub frmPrincipal_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Quit", "Really want to exit?", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
'App end.
server.Stop()
Else
'User decides to stay.
e.Cancel = True
End If
End Sub
Does anybody knows how to avoid the WebBrowser (GEWebBrowser in this case) to execute the refresh method? If anyone knows how to solve this in any other way, I would still be very grateful.
You could try this (not guaranteed to work):
Private Sub StopRefresh(sender As System.Object, e As System.EventArgs) Handles WebBrowser1.ProgressChanged
WebBrowser1.Stop()
End Sub

Application.Exit() and FormClosing event in Vb.net

I have a single windows form application that is running in system tray icon.If the user press X button of the windows form a messagebox is displayed with Yes and No ( Yes ->close the form---No->keep the form running in system tray icon).
I was thinking to prevent the scenario when the user open another instance of the application when there is already an instance running so i have used this code :
If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length> 1 Then
MessageBox.Show("Another instance is running", "Error Window", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
Application.Exit()
End If
The problem is that when i want to test this the message is displayed but after i press ok, a new messagebox appears (that one from Private Sub Form_FormClosing ).If i choose NO i will have to instance running!
I have read that Application.Exit fires the Form_FormClosing event.
Is there any possibility to cancel the triggering of the Form_FormClosing event,or am i doing something wrong?
'this is the formclosing procedure
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Try
Dim response As MsgBoxResult
response = MsgBox("Are you sure you want to exit", CType(MsgBoxStyle.Question + MsgBoxStyle.YesNo, MsgBoxStyle), "Confirm")
'If the user press Yes the application wil close
'because the application remains in taskmanager after closing i decide to kill the current process
If response = MsgBoxResult.Yes Then
Process.GetCurrentProcess().Kill()
ElseIf response = MsgBoxResult.No Then
e.Cancel = True
Me.WindowState = FormWindowState.Minimized
Me.Hide()
NotifyIcon1.Visible = True
End If
PS: I am not a programmer so please don't be to harsh with me:)
You don't need to Kill the current process or use the End Statement. If you have to use these then there is something amiss with your application.
When you want to end your application use Me.Close. This will fire the FormClosing event:
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select Case MessageBox.Show("Are you sure you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case Windows.Forms.DialogResult.Yes
'nothing to do here the form is already closing
Case Windows.Forms.DialogResult.No
e.Cancel = True 'cancel the form closing event
'minimize to tray/hide etc here
End Select
End Sub
To stop more than one copy of your application from running use the option to Make Single Instance Application
In the situation where you are just starting your application and are testing for previous instances I have used the VB End Statement to terminate the application.
The End statement stops code execution abruptly, and does not invoke
the Dispose or Finalize method, or any other Visual Basic code. Object
references held by other programs are invalidated. If an End statement
is encountered within a Try or Catch block, control does not pass to
the corresponding Finally block.
If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length> 1 Then
MessageBox.Show("Another instance is running", "Error Window", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End
End If
Private Sub main_master_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
'Put you desired Code inside this!
Msgbox("Application Closing from Taskbar")
End If
End Sub
It will Close the exe from Taskbar or kill Process. If user Close the
Application from taskbar.
CloseReason.UserClosing
event will close the application if it is closed by User from
Taskber

VB app not ending

I am using Visual Basic 2010 and I am makeing a simple login app it prompts you for a username and password if it gets it right I want to open a form then close the previus one but when I do me.close it ends the program and I can't go on
I am positive its working because I get the right password and username ut I can't close the first windows
I tried to hide it but when I do I cant close it and the program goes on without quiting and with a hidden form
I heard rumers that there is Sub that can control the x in the corner
if there is one that would probably solve my problem but I can't find it heres the code for the login form
Public Class Main_Login
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "My Name" And TextBox2.Text = "mypassword" Then
Contentsish.Show()
Me.Hide()
Label3.Hide()
Else
Label3.Show()
End If
End Sub
End Class
Then the Form if you enter the right login info
Public Class Contentsish
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Hide()
Timer1.Stop()
End Sub
End Class
how can I fix this problem?
Go into your project settings and set the application to close when the last form closes and not when the startup form closes.
edit: I just checked some of my VB.Net 2k8 code. What I do is create a new instance of the "child" form, do any initialization that I need, call .Show() on it, and then call .Close() on the current form (Me.Close()). Probably not the best way to do things, but it works for me. This is with the project setting I described earlier set. This allows me to exit from the child form or "logout" if needed.
Two other ways you can do this, in addition to Crags:
Load the main form first, then load the password from the main form.
You can use a separate vb module and use the Sub Main for the startup (you specify this in Project Properties, Application, Startup Object), then load the two forms from Sub Main.
So what happens if the login is incorrect? Sounds like you might want to show the login form using ShowDialog, maybe in your Main() before calling Application.Run(new FormMain());