473,508 Members | 4,179 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dispose which doesn't "dispose"

Hi

I have a class which implements IDisposable, but it doesn't really use
the Dispose method for cleaning up resources, it uses it for completing
a calculation.

Actually I think it seems quite smart (I think there are other problems
with the class though) but I was wondering if this was stretching the
use/meaning of IDisposable a bit?

The class is used as part of a module for timing method calls, and you
would use it like:

using (MyTimer timer = new MyTimer("timername"))
{
// Do stuff which needs timing....

} // At the end of the "using", the MyTimer stops its timing
// Get some timing statistics from static method:
double msec = MyTimer.GetMilliseconds("timername");

regards,
Peter
Sep 10 '08 #1
5 1437
Peter.. It is always a good idea when you try to stretch your mind like
this. I do
wonder if the same functionality can be accomplished with a try finally
block
which may be simpler since there is no need for the timer to implement
IDisposable. So the timer, if valid, would be stopped in the finally
block.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Sep 10 '08 #2
Peter <xd****@hotmail.comwrote:
I have a class which implements IDisposable, but it doesn't really use
the Dispose method for cleaning up resources, it uses it for completing
a calculation.

Actually I think it seems quite smart (I think there are other problems
with the class though) but I was wondering if this was stretching the
use/meaning of IDisposable a bit?

The class is used as part of a module for timing method calls, and you
would use it like:

using (MyTimer timer = new MyTimer("timername"))
{
// Do stuff which needs timing....

} // At the end of the "using", the MyTimer stops its timing
// Get some timing statistics from static method:
double msec = MyTimer.GetMilliseconds("timername");
It's stretching it, but I've seen similar things before. An alternative
is to use a closure:

timer.Time(() =>
{
// Do stuff which needs timing
});

// Get some timing statistics from static method:
double msec = MyTimer.GetMilliseconds("timername");

(For C# 2 you'd use an anonymous method instead - not much difference
in this case.)

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 10 '08 #3
using (mocks.Record())
{
}

using (mocks.ReplayAll())
{
}
I think that's how I saw it done anyway :-)
Sep 10 '08 #4
Jeff Louie wrote:
Peter.. It is always a good idea when you try to stretch your mind
like this. I do
wonder if the same functionality can be accomplished with a try
finally block
which may be simpler since there is no need for the timer to
implement IDisposable. So the timer, if valid, would be stopped in
the finally block.
In this case, the Dispose method performs some work on internal
variables which cannot be accessed externally. However, one can of
course use a try-catch-finally and call "Dispose" manually.

I think the idea behind using "using" was to ease the use of the class,
and make it "look tidy". The client class does need to ensure it uses
the timer class correctly, but then that sort of "restriction" is
relatively normal.

/Peter
Sep 11 '08 #5
Peter... As you probably know C++ coders like to place initiation code
in the
constructor and do clean up in the destructor for two reasons. First,
C++ has
deterministic destructors so that it will be called when the object goes
out of
scope. Second, the destructor of a fully constructed object will be
called even
if an exception is thrown.

A similar pattern can be applied in C# with IDisposable and using.
However,
unlike C++, the finalizer (destructor) will be called in C# even if an
exception
is thrown in the constructor so you would need to code appropriately.

IMHO, this still begs the question of applying this pattern to stop a
timer as
opposed to:

{
Timer t= new Timer();
t.Start();
...
t.Stop();
}

If the timer must be stopped even if an exception is thrown, then indeed
it
seems to me that:

using (Timer t= new Timer())
{
...
}

is preferable over:

Timer t= new Timer();
try {
t.Start();
...
}
finally {
t.Stop();
}
Regards,
Jeff
>>In this case, the Dispose method performs some work on internal
variables which cannot be accessed externally. However, one can of
course use a try-catch-finally and call "Dispose" manually.

I think the idea behind using "using" was to ease the use of the class,
and make it "look tidy". The client class does need to ensure it uses
the timer class correctly, but then that sort of "restriction" is
relatively normal.
<<

*** Sent via Developersdex http://www.developersdex.com ***
Sep 11 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4157
by: leegold2 | last post by:
Is there a way I can "dispose" of an array. For example I need to use an array for short time in a script. After I've used that data structure I want to dispose of it to free-up resources. Is there...
14
3618
by: Atara | last post by:
I know in C++ it is true. but what about VB .Net and its GarbageCollector ? For example, consider the following case. Does f2() needs to do any disposing code ? Public Sub f1() As...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5635
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.