How use instance's fields immediatelly before they are destroyed

  • Thread starter Thread starter Naldo Alcalde
  • Start date Start date
N

Naldo Alcalde

Hi friends, that's my question. I need use the instance's fields in my
last process before they are destroyed. Similar to a C++ destructor, because
C# destructor is called after of destroy the fields.

Thx in advance,

Naldo Alcalde H.
 
Naldo Alcalde said:
Hi friends, that's my question. I need use the instance's fields in my
last process before they are destroyed. Similar to a C++ destructor, because
C# destructor is called after of destroy the fields.

No, the C# finalizer is called at some point which may be before or
after the finalizer is called on the objects referred to by the fields.
There's no guarantee that it will be afterwards (and indeed there can't
be - there could be cyclic references).

Having to use a finalizer at all is usually a sign of a design which
needs rethinking though - what exactly are you trying to do?
 
Hi, Jon, thx for you answer. I'm implementing a Log class that writes
the initial message for example

"*** Log Started at yyyy-MMM-dd hh:mm:ss.fff ***"

and want the similar for the last message

"*** Log Stopped at yyyy-MMM-dd hh:mm:ss.fff ***"

for that reason I want write this message as last processing.

Best regards,

Naldo Alcalde H.
 
Naldo Alcalde said:
Hi, Jon, thx for you answer. I'm implementing a Log class that writes
the initial message for example

"*** Log Started at yyyy-MMM-dd hh:mm:ss.fff ***"

and want the similar for the last message

"*** Log Stopped at yyyy-MMM-dd hh:mm:ss.fff ***"

for that reason I want write this message as last processing.

Is the idea that you only want to stop when the application finishes?
If so, you could possibly use the AppDomain.DomainUnload or
AppDomain.ProcessExit events.
 
Yep, the stop message must be written when the application finishes.

Use the events that you have mentioned will be useful if my log
instances has
global scope, but that not always is the case.

I think that the instances itself must have some way to do this.

Naldo Alcalde H.
 
Naldo Alcalde said:
Yep, the stop message must be written when the application finishes.

Use the events that you have mentioned will be useful if my log
instances has global scope, but that not always is the case.

I think that the instances itself must have some way to do this.

Well, the best way would be to shut the application down cleanly,
telling each object which cares about the lifetime that it's about to
shutdown. Relying on a finalizer isn't a nice way of doing this.
 
Thx a lot Jon for your help.

Naldo Alcalde H.

Jon Skeet said:
Well, the best way would be to shut the application down cleanly,
telling each object which cares about the lifetime that it's about to
shutdown. Relying on a finalizer isn't a nice way of doing this.
 

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

Back
Top