Determining the reason for Dispose at the end of 'using' block

  • Thread starter Thread starter Menny
  • Start date Start date
M

Menny

Hi,
I'm looking for a way to determine if the 'Dispose()' function at the
end of a 'using' block, was called due to an exception.

Can anyone help?
 
Your best bet might be to explain what you are trying to do. I don't
remember to have seen such a requirement. Though you could likely use a flag
or whatever it might be better to see if this is really needed...
 
In several places in my code, I have this 'using' block:
****************************************
using(new MyDisposer())
{
//some code
....
}
****************************************
The class MyDisposer is:
****************************************
class MyDisposer : IDisposable
{
public void Dispose()
{
//does cleanups and automatic saving
....
}
}
****************************************

It all works fine, I really like this automatic calls at the end of the
stack.
But, I should not do automatic saving in case an Exception was thrown.
Is there any 'if' I can do before saving, to make sure that the stack
did not ended due to an Exception?
 
Menny,

What about something like this.

using (MyDisposer disposer = new MyDisposer())
{
try
{
// Do whatever you need to do here.
}
catch
{
disposer.CancelAutomaticSaving();
}
}

Brian
 
Thanks Brian,

This is a possibility, and it will work, but I'm looking for an
automatic solution. I have A LOT of places in the code which uses this
'using' pattern.
I'll have to go over all of them, and add this piece of code.

If I'll want to use your solution, I'll create a Guarding function:
*********************************
public void GaurdFunction(Delegate d)
{
using (MyDisposer disposer = new MyDisposer())
{
try
{
d.Invoke()
}
catch
{
disposer.CancelAutomaticSaving();
throw;
}
}
*********************************
I don't like this solution....

Any other ideas?
 
Another option could be just to perform this cancellation from the Invoke
method so that you don't have to change the calling code.

As a side note, my personal preference would be likely to avoid putting
application specific features in the dispose method.
 
Patrice,
could you elaborate?


Another option could be just to perform this cancellation from the Invoke
method so that you don't have to change the calling code.

As a side note, my personal preference would be likely to avoid putting
application specific features in the dispose method.
 
This is just a variation around Brian's suggestion. Instead of doing this
job in the code that calls the Invoke method, the idea would be to do this
inside the Invoke method itself.

This way you should have only the Invoke and Dispose method to change
(instead of having to change all the code that calls into these methods).

It would give bascially a new Invoke method such as :

public void Invoke()
try
{
this.oldInvokeImplementation()
}
catch
{
this.PrivateAutoSaveFlag=false;
throw;
}
}

--
Patrice

"Menny" <[email protected]> a écrit dans le message de (e-mail address removed)...
Patrice,
could you elaborate?
 
Hello Menny,

M> I'm looking for a way to determine if the 'Dispose()' function at the
M> end of a 'using' block, was called due to an exception.
M> Can anyone help?

The only way is to avoid using "using" keyword and to unwrap it explicitly
- try {} finally {} and set flag/log dispose call.

I suggest to declare your object before the try block and init within - the
reason is that in "using" keyword initialization have place outside the try{}
and if some exeption arised during initialization finally is not reached
and disposed is not called

---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Menny said:
In several places in my code, I have this 'using' block:
****************************************
using(new MyDisposer())
{
//some code
....
}
****************************************
The class MyDisposer is:
****************************************
class MyDisposer : IDisposable
{
public void Dispose()
{
//does cleanups and automatic saving
....
}
}
****************************************

It all works fine, I really like this automatic calls at the end of the
stack.
But, I should not do automatic saving in case an Exception was thrown.
Is there any 'if' I can do before saving, to make sure that the stack
did not ended due to an Exception?

My preference would be to have a "Commit" method which did the saving.
This would mirror the normal use of transactions in database classes,
where if you call Commit() before disposing, it commits and then the
disposal does nothing, whereas if you just dispose it rolls back.

Your calling method would then be:

using(MyDisposer whatever = new MyDisposer())
{
//some code
....
whatever.Commit();
}
 
Back
Top