CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Join Date
    Jul 2003
    Posts
    72
    Ideally you should put this functionality in a service add write to it via shared memory. The event log is sort of one example of this but has limitations.

  2. #17
    Join Date
    Jul 2002
    Location
    EU
    Posts
    68
    Martin,

    Thanks a lot for your answer!

    It seems that I found the right person to ask questions

    It so happens, that I have to write a trace-log class. I have to trace function calls - too fast for TimeStamp to be of any use, but that's another problem, and I solved it somehow.

    Anyway, the problem is this: There are hundreds of thousands of messages to be logged, during the whole execution of the application. I keep the file open, because otherwise the overload for opening/closing would be exceedingly high. I am very upset because it is not properly closed. There is no method explicitly called when the application exists, therefore the only place where I could put the close is in Dispose; which is never called!!!

    Perhaps you have an idea...

    Thanks,
    Petru

  3. #18
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    Hm... Sure, place it to Dispose() method...

    However Dispose() is called from the destructor. We are dealing with garbage collector in .net so destructor calls are not deterministic...

    Garbage collector examines so-called application roots maintained by the CLR. When the object is not reacheble from any application roots it is free to deletion. The process of deletion is called Garbage Collecting. (it is not just about deleting unreachable objects. It is also about moving objects among the generations - internal structure of gargbage collector) However you don't exactely know when that occures. It may be immediatelly after you close tha application or even half a hour after quitting your app...

    Fortunatelly you can force this garbage collecting. There is a class GC in System namespace. You can use System.GC.Collect() method to perform the garbage collecting immediatelly. It will imediatelly call destructor and Dispose() method of that object.

    You need to handle request to quit the application. And you can call GC.Collect() there...

    martin

  4. #19
    Join Date
    Sep 2003
    Posts
    10
    well may be i m too late but just wanted to share somthing which you guys might be knowing already..but for those who dont..

    we actually dont need to invoke the garbage collector manually just for the heck of calling dispose() method of any particular class.. a good solution will be to use using statement like this..

    using (Resource res = new Resource()) {
    res.DoWork();
    }

    the above code is as good as the following..

    Resource res = new Resource(...);
    try {
    res.DoWork();
    }
    finally {
    if (res != null) ((IDisposable)res).Dispose();
    }

    so it calls the dispose() method automatically when u are done with ur object.

    -Harshit

  5. #20
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    Yes, it is true... However it works only when you exactelly know where you object does the job... It is impossible to use if the object should be alive during the whole programe execution and should be destroyed before you program finishes...

    martin

  6. #21
    Join Date
    Oct 2003
    Posts
    9

    Overhead for opening and closing file?

    Isn't there some overhead for opening and closing file every time application writes something?

    I have a very hihg performance required console application that does a lot of logging. Is it going to slow it down if log file is opened aand losed frequently?

    Thanks.

  7. #22
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    Posting my two cents on the "keep open"/"open on demand" issue. I have faced this many times. There are issues with both.

    For a top performance class, I will typically use a timer. A write to the file resets the timer. If the timer expires, i close the file.

    My timer is typically on the order of 1 second.

    Now the file is closed "most of the time", bu remains open between successive calls within a short duration.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured