App not exiting

tomhosking

Well-known member
Joined
Aug 4, 2005
Messages
49
Programming Experience
1-3
When I click the close button on the main form of my project, it closes, but the application seems to stay running, as it appears in the task manager. Is there something obvious I should be doing that Im not?
 
Didnt work :(

My Closing event sub:
VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Form1_Closing([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.ComponentModel.CancelEventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] [/size][size=2][color=#0000ff]MyBase[/color][/size][size=2].Closing

[/size][size=2][color=#0000ff]If[/color][/size][size=2] clsCommon.Flags.connected = [/size][size=2][color=#0000ff]True[/color][/size][size=2] [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]clsCommon.thisSession.disconnect()

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

 

[/color][/size][size=2][/size][size=2][color=#008000]' Write the log to file

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] sr [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.IO.StreamWriter = System.IO.File.CreateText(Application.StartupPath & "\Log.txt")

sr.WriteLine("Beginning of log file - written on " & Now.ToString())

sr.Write(txtDebugBox.Text)

sr.WriteLine("End of log file - written on " & Now.ToString())

sr.Close()

Application.Exit()

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size]
[size=2][color=#0000ff]
[/color][/size]

What do I need to do to make sure Ive destroyed all the objects? I thought the garbage collector destroyed them automatically as soon as they went out of scope...
 
I spoke too soon, it actually raises an exception!

The problem appears to be that mscorlib doesnt want to exit. What does this dll do, and what might it be waiting for?
 
Its not mscorlib...

To allow interactions between forms, my main form is a shared member of a class. In a basic setup, doing this causes the form to be unable to close. However, interactions between forms are fundamental to what the program does, so I cant disable it!

Setting the shared member to Nothing, or Dispose()ing it doesnt help...
 
This Might Work

Try

Application.ExitThread()

on your other thread make sure that you have isBackground set.

this setting will cause the thread to exit when the application exits and not cause the process to continue running.


"A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process by invoking Abort on any background threads that are still alive."
 
You should not use END

There are 2 reasons you generally don't want to use End.

1) It requires UnmanagedCode permissions for your application, this may or may not be an issue depending on the environment that you are working with.
2) It does not invoke finalizers or cause ending events in your code.
 
Would the first problem appear if I ran the complied app in a certain environment? Is there any reason I would need to invoke finalizers if I hadnt put anything in them?

I did try using ExitThread(), but the problem wasn't the multithreading, it was the presence of a shared form object in a class. Setting this to Nothing didnt help...
 
Back
Top