Dispose, finalize and singleton classes.

B

BLUE

Since singleton classes conceptually are like static classes, the are
supposed to last for the entire lifetime of the application.

Starting from this point tell me if I'm wrong saying:
- it make no sense to implement IDisposable
- it make sense to implement Finalize if there are unmanaged resources, else
the GC will not be able to free them
- this is perhaps the only case in which a finalizer is implemented, but a
Dispose is not implemented

If I've a thread in a singleton class, before closing the application I have
to wait for thread end (some way telling the thread it should stop as soon
as possible): could this be done into the Closing or Close event handler of
the main form?


Thanks,
Luigi.
 
M

Mr. Arnold

If I've a thread in a singleton class, before closing the application I
have to wait for thread end (some way telling the thread it should stop as
soon as possible): could this be done into the Closing or Close event
handler of the main form?

If the thread.IsAlive, then at the form Close event is where you need to
stop the thread and end the program.
 
S

Samuel R. Neff

You could set an instance variable like "KeepRunning" on the class
with the thread and within your main thread loop you do

while(KeepRunning) {
...
}

and then in the Close event you set singleton.KeepRunning = false;

Another option is to set the thread to be a Background thread in which
case it does not have to be shut down manually. You won't get as
clean of an exit from the thread, so which you choose depends entirely
on what the thread actually does.

Sam
 

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