Search code examples
vb.netfinalizer

Finalize calling class method


I have a logging class that stores entries in a datatable dt. I then use SQLBULKCOPY to write that dt out to a sql table. Basic stuff. Problem is, I'd like to only call SQLBULKCOPY when there's say 50 entries in the dt. The problem is, what if I'm done (either intentionally or not, like if the code block that's using the log class throws an exception) with the logging object and there are still 15 rows in the dt?

What I'd like to do is have some "finalize" code in the log class itself that calls the log class's own method that writes those 15 records from the dt using sqlbulkcopy. I know overriding Dispose/Finalize methods really isn't meant for my situation. And it isn't feasible to use some kind of external-to-the-logging-class way, like giant try/catches that always can call a fake finalize method. (Reason being there might be 4 or 5 layers of nested classes with numerous try/catches, using's, etc.)

Any ideas?


Solution

  • Why not implement the IDisposable pattern?

    If you are intentionally shutting down, you can call .Dispose, and if you are unexpectedly shutting down, you can clean up in .Dispose(False).