Application.Exit() Don't exit right away!

I

iDesmet

Good day to Everybody!


I have searched in Internet (mostly in Google) about the problem I
have and didn't find any answer or tip. Maybe I made the wrong
questions.

Anyway, the problem I have is the following:

My application starts, it shows a splash screen with some text that
tells the user what's going on. It stops if there's an error.

Everything oke here, but in the MainForm_Load I have some procedures I
call before showing the MainForm. Such procedures are made to: read
the configuration file, license, etc.

If there's an error, I show it to the user (MessageBox) and then I
call Application.Exit()

The problem is that it doesn't exit right away, it's like it keeps
reading the remaining lines in the MainForm_Load and then exits.

It shows the MainForm and then a MessageBox (I suppose this is the
MessageBox's Exit Confirmation Window I created in MainForm_Closing)
and exits quickly; all this like about 1 second.

So, I was wondering if someone could show me the way to fix this
little problem. I don't want to show the Windows, I want the
application exit right away.

I haven't posted my code but, if it's necessary please don't bother to
ask!

Ohh btw, I'm using VB.NET and Windows Vista if such information is
important.

Thanks in advance!

PS My apologies for my bad english

Regards,
iDesmet
 
K

kimiraikkonen

Good day to Everybody!

I have searched in Internet (mostly in Google) about the problem I
have and didn't find any answer or tip. Maybe I made the wrong
questions.

Anyway, the problem I have is the following:

My application starts, it shows a splash screen with some text that
tells the user what's going on. It stops if there's an error.

Everything oke here, but in the MainForm_Load I have some procedures I
call before showing the MainForm. Such procedures are made to: read
the configuration file, license, etc.

If there's an error, I show it to the user (MessageBox) and then I
call Application.Exit()

The problem is that it doesn't exit right away, it's like it keeps
reading the remaining lines in the MainForm_Load and then exits.

It shows the MainForm and then a MessageBox (I suppose this is the
MessageBox's Exit Confirmation Window I created in MainForm_Closing)
and exits quickly; all this like about 1 second.

So, I was wondering if someone could show me the way to fix this
little problem. I don't want to show the Windows, I want the
application exit right away.

I haven't posted my code but, if it's necessary please don't bother to
ask!

Ohh btw, I'm using VB.NET and Windows Vista if such information is
important.

Thanks in advance!

PS My apologies for my bad english

Regards,
iDesmet

Hi,
As you're doing stuff in Form_load event, that is raised before main
form is displayed, in form's load event put all the stuff in a try-
catch block, and try to call Application.Exit in Catch block to exit
when an exception occurs:

In form_load event handler:
Try

' Do stuff
' read the configuration file, license, etc.

Catch ex As Exception

' You can customize messagebox
' content and messagebox style
Msgbox(....)

' Ready to call application.exit
' based on MsgBox answer
Application.Exit()

End Try

If the above doesn't work, try to use Me.Hide() in Catch block to
force form to be hidden then allow Application.Exit to exit after
Messagebox confirmation.

Hope this helps,

Onur Güzel
(e-mail address removed)
(e-mail address removed)
 
A

Armin Zingler

iDesmet said:
Everything oke here, but in the MainForm_Load I have some procedures I
call before showing the MainForm. Such procedures are made to: read
the configuration file, license, etc.

If there's an error, I show it to the user (MessageBox) and then I
call Application.Exit()

Is it right that you don't want to show the Form if an error occurs? Then
the implementation of what you want is:

try
initialization
catch
handle error and
return
end try

show form

Put it in sub main.


Armin
 
J

Joe Cool

Good day to Everybody!


I have searched in Internet (mostly in Google) about the problem I
have and didn't find any answer or tip. Maybe I made the wrong
questions.

Anyway, the problem I have is the following:

My application starts, it shows a splash screen with some text that
tells the user what's going on. It stops if there's an error.

Everything oke here, but in the MainForm_Load I have some procedures I
call before showing the MainForm. Such procedures are made to: read
the configuration file, license, etc.

If there's an error, I show it to the user (MessageBox) and then I
call Application.Exit()

The problem is that it doesn't exit right away, it's like it keeps
reading the remaining lines in the MainForm_Load and then exits.

It shows the MainForm and then a MessageBox (I suppose this is the
MessageBox's Exit Confirmation Window I created in MainForm_Closing)
and exits quickly; all this like about 1 second.

So, I was wondering if someone could show me the way to fix this
little problem. I don't want to show the Windows, I want the
application exit right away.

I haven't posted my code but, if it's necessary please don't bother to
ask!

Ohh btw, I'm using VB.NET and Windows Vista if such information is
important.

Thanks in advance!

PS My apologies for my bad english

Regards,
iDesmet

I have been bitten by this more than once. Application.Exit() does not
force the application to exit then and there. It schedules the exit
for when the current code path ends. So if you invoke this method in,
say, a control's event handler, then you should code it so that
nothing else happens in the event handler after invoking it. For
example, if the Application.Exit() is being invoked because some
object failed to initialize and you try to access that object later in
the same event handler, then your application will croak when it
attempts to access an object with no value yet.

public sub someeventhandler

If <some condition is true that indicates the app should exit> then
<display some message>
Application.Exit()
else
<do what you want to do if the app does not need to exit>
end if

end
 
I

iDesmet

I replaced Application.Exit() with Dispose() and it worked!

I'm wondering why I needed to use Dispose() instead of Application.Exit
() ?...

Is it because in frmMainMDI_Closing I ask if the user is really sure
that he/she want's to exit or because in frmMainMDI_FormClosed I call
Application.Exit()?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top