By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,515 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

How use instance's fields immediatelly before they are destroyed

P: n/a
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.
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Naldo Alcalde <li*************@hotmail.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Naldo Alcalde <li*************@hotmail.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3

P: n/a
Naldo Alcalde <li*************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

P: n/a
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.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Naldo Alcalde <li*************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5

P: n/a
Naldo Alcalde <li*************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

P: n/a
Thx a lot Jon for your help.

Naldo Alcalde H.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Naldo Alcalde <li*************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.