Problems Closing Singleton Application

  • Thread starter Thread starter Beebs
  • Start date Start date
B

Beebs

I seem to be having problems closing a singleton application I created
from a form's exit menu. If I have this code in my mnuExit event:

Me.Dispose()
frmStartup.Instance().Close()

It only closes (or disposes) the current form and the frmStartup form.

If I use this code in my mnuExit event:

frmForm1.Instance().Dispose()
frmForm2.Instance().Dispose()
frmForm3.Instance().Dispose()
frmForm4.Instance().Dispose()
frmStartup.Instance().Close()

it takes a long time to dispose of the forms, but for some reason (and
even if I use frmStartup.Instance().Dispose) the frmStartup form is
still visible!!! What do I have to do to close down a singleton
application. By the way, in my modMain, I use the following code to
launch the app:

Application.Run(New frmStartup)
 
Unless you are showing the forms with ShowDialog, you don't need to
explicitly dispose them; just call the Close method.

Do that for all your forms and leave last the main form (in your case
frmStartup since that is what you pass to Application.Run). As your last
call Close the main form. BTW, I have no idea why you have an Instance
method on your main form since you are creating it in the call to
Application.Run [the singleton pattern with the Instance property assumes
all constructors are private and hence the object/form is not creatable from
anywhere else other than within the Instance property].

Apart from closing the main form, you should make sure that any threads you
have implicitly or explicitly created have stopped.

If you are still having issues post a small repro so we can look at it.

Cheers
Daniel
 
Unless you are showing the forms with ShowDialog, you don't need to
explicitly dispose them; just call the Close method.

Do that for all your forms and leave last the main form (in your case
frmStartup since that is what you pass to Application.Run). As your last
call Close the main form. BTW, I have no idea why you have an Instance
method on your main form since you are creating it in the call to
Application.Run [the singleton pattern with the Instance property assumes
all constructors are private and hence the object/form is not creatable from
anywhere else other than within the Instance property].

Apart from closing the main form, you should make sure that any threads you
have implicitly or explicitly created have stopped.

If you are still having issues post a small repro so we can look at it.

I can't do this now:

frmStartup.Close()

because I would have to create a new instance of it just to close it
and that doesn't seem to make much sense. Also, I can't inherit that
into my form's class cause that throws an error about only one inherit
is allowed, etc...so how do I call the startup form to close then if I
remove it's instance method, which I have since done?
 
I can't do this now:
frmStartup.Close()

because I would have to create a new instance of it just to close it
and that doesn't seem to make much sense. Also, I can't inherit that
into my form's class cause that throws an error about only one inherit
is allowed, etc...so how do I call the startup form to close then if I
remove it's instance method, which I have since done?

Ok, I think I got that figured out, but here's my exit menu's code:

Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuExit.Click
Dim startup As frmStartup

frmForm1.Instance().Dispose()
frmForm2.Instance().Dispose()
frmForm3.Instance().Dispose()
frmForm4.Instance().Dispose()
startup.Close()
End Sub

and now I'm getting a null reference exception error occurring on the
startup.Close() line and before that it takes my application forever
to close out the other screens...surely there has to be a better way
for this to work...any further suggestions?
 
so how do I call the startup form to close then if I
remove it's instance method, which I have since done?

Good that you removed the Instance method, I didn't see a point in that.
Your exit menu is part of the frmInstance right? So all you have to do in
the event handler is call Me.Close() as the last statement.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Beebs said:
Unless you are showing the forms with ShowDialog, you don't need to
explicitly dispose them; just call the Close method.

Do that for all your forms and leave last the main form (in your case
frmStartup since that is what you pass to Application.Run). As your last
call Close the main form. BTW, I have no idea why you have an Instance
method on your main form since you are creating it in the call to
Application.Run [the singleton pattern with the Instance property assumes
all constructors are private and hence the object/form is not creatable
from
anywhere else other than within the Instance property].

Apart from closing the main form, you should make sure that any threads
you
have implicitly or explicitly created have stopped.

If you are still having issues post a small repro so we can look at it.

I can't do this now:

frmStartup.Close()

because I would have to create a new instance of it just to close it
and that doesn't seem to make much sense. Also, I can't inherit that
into my form's class cause that throws an error about only one inherit
is allowed, etc...so how do I call the startup form to close then if I
remove it's instance method, which I have since done?
 
You didn't initialize the "startup" variable, you just declared it.

If I do this:

Dim startup As New frmStartup

then the form is still visible after all the other ones have closed
since I had to initialize it in my Application.Run call as well, which
means I now I have two instances and only the new one gets closed, not
the one that started the application. Maybe I don't understand basic
VB .NET syntax cause this sure seems backwards...
 
Good that you removed the Instance method, I didn't see a point in that.
Your exit menu is part of the frmInstance right? So all you have to do in
the event handler is call Me.Close() as the last statement.

Sorry Daniel, but I'm using this code on other forms, not the
frmStartup, so Me.Close will not close the startup form, only the
current form that is active...see my previous posts for my current
problem...I guess I'm just not getting something.
 
OK, you have now given us more info.

The clean way is to have the exit menu on your main form only and expect
users to close any other forms until they reach the "main" form (at which
point Me.Close will work - or simply setting the form's MinimizeBox to
False).

A not so clean approach is to use Application.Exit from any place in your
app - not my preference and I never use it personally.

Other hacks will no doubt work but then you are off the beaten path...

Cheers
Daniel
 
The clean way is to have the exit menu on your main form only and expect
users to close any other forms until they reach the "main" form (at which
point Me.Close will work - or simply setting the form's MinimizeBox to
False).

A not so clean approach is to use Application.Exit from any place in your
app - not my preference and I never use it personally.

Other hacks will no doubt work but then you are off the beaten path...

Thanks Daniel, that is the kind of advice I look forward to in this
group. I in no way want my applications to be different from the
conventional GUI navigation that most users are accustomed to so I
think I'll go with your suggestion, it definitely makes sense to me.
 
Back
Top