Using Using object disposal...
-
Cheers! :-D
-
Check this code. Two questions: 1. After opening the serial port do I need to worry about closing the port as when I return (leave the using scope) the object will automatically be disposed? 2. If the code enters the catch block will the serial port object automatically be disposed after leaving the using scope? Thanks!
public static bool IsPortInUse(string portName) { try { using(SerialPort sp = new SerialPort(portName)) { sp.Open(); return false; } } catch (UnauthorizedAccessException e) { return true; } }
If the code execution leaves the
using
scope for any reason, the object'sDispose
method is automatically called and the object free'd.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
If the code execution leaves the
using
scope for any reason, the object'sDispose
method is automatically called and the object free'd.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Thanks for your reply. I was pretty sure that was right. :-D Hey, but if an object doesn't implement IDisposable is there any point in using Using?
-
Thanks for your reply. I was pretty sure that was right. :-D Hey, but if an object doesn't implement IDisposable is there any point in using Using?
TheTinSoldier wrote:
if an object doesn't implement IDisposable is there any point in using Using?
If it doesn't implement IDisposable, you will get a compiler error if you try to use it in a
using
block.----------------------------- In just two days, tomorrow will be yesterday. http://geekswithblogs.net/sdorman
-
Check this code. Two questions: 1. After opening the serial port do I need to worry about closing the port as when I return (leave the using scope) the object will automatically be disposed? 2. If the code enters the catch block will the serial port object automatically be disposed after leaving the using scope? Thanks!
public static bool IsPortInUse(string portName) { try { using(SerialPort sp = new SerialPort(portName)) { sp.Open(); return false; } } catch (UnauthorizedAccessException e) { return true; } }
It actually compiles to something like this:
public static bool IsPortInUse(string portName) {
try {
SerialPort sp = null;
try {
sp = new SerialPort(portName);
sp.Open();
return false;
} catch (Exception ex) {
throw ex;
} finally {
if (sp != null) {
sp.Dispose();
}
}
} catch (UnauthorizedAccessException e) {
return true;
}
}Which will automatically dispose, even if an exception occurs.
Try code model generation tools at BoneSoft.com.
-
Check this code. Two questions: 1. After opening the serial port do I need to worry about closing the port as when I return (leave the using scope) the object will automatically be disposed? 2. If the code enters the catch block will the serial port object automatically be disposed after leaving the using scope? Thanks!
public static bool IsPortInUse(string portName) { try { using(SerialPort sp = new SerialPort(portName)) { sp.Open(); return false; } } catch (UnauthorizedAccessException e) { return true; } }
Some comments: 1. you dont need to open a port to know whether it is already open; use the IsOpen property. 2. I would have nested the try and using blocks the other way around, but I dont think it matters much. 3. I did not find in MSDN doc a statement indicating that Dispose() also calls Close(); lets hope it does. Other classes have either a Dispose() or a Close() method but not both (of course you could Open() again after Close, not after Dispose). 4. SerialPort.Close (or Dispose?) does not immediately close the port, so when soon followed by a SerialPort.Open that open may fail (see second remark in MSDN doc on Close). As a result, while your method name sounds like it is only providing information, you are actually changing its state (albeit temporarily). :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Some comments: 1. you dont need to open a port to know whether it is already open; use the IsOpen property. 2. I would have nested the try and using blocks the other way around, but I dont think it matters much. 3. I did not find in MSDN doc a statement indicating that Dispose() also calls Close(); lets hope it does. Other classes have either a Dispose() or a Close() method but not both (of course you could Open() again after Close, not after Dispose). 4. SerialPort.Close (or Dispose?) does not immediately close the port, so when soon followed by a SerialPort.Open that open may fail (see second remark in MSDN doc on Close). As a result, while your method name sounds like it is only providing information, you are actually changing its state (albeit temporarily). :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
It does not also call
Close()
, onlyDispose()
. But most things do callClose()
in their implementation ofDispose()
.
Try code model generation tools at BoneSoft.com.
-
Some comments: 1. you dont need to open a port to know whether it is already open; use the IsOpen property. 2. I would have nested the try and using blocks the other way around, but I dont think it matters much. 3. I did not find in MSDN doc a statement indicating that Dispose() also calls Close(); lets hope it does. Other classes have either a Dispose() or a Close() method but not both (of course you could Open() again after Close, not after Dispose). 4. SerialPort.Close (or Dispose?) does not immediately close the port, so when soon followed by a SerialPort.Open that open may fail (see second remark in MSDN doc on Close). As a result, while your method name sounds like it is only providing information, you are actually changing its state (albeit temporarily). :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
Hi Luc. In regards to your points: 1. I need to first open the port to call the IsOpen property. I am checking if the port is in use. (not by my application). 2. It would be nice to know if it does matter, as I'll do it your way if it's the right way 3. I get the feeling (from you and others) that even though the object will be disposed it wouldn't hurt to close it first. 4. This is interesting, I'd better check that out. I think for what I'm doing it should be ok. Thanks to you and everyone for their help!
-
It does not also call
Close()
, onlyDispose()
. But most things do callClose()
in their implementation ofDispose()
.
Try code model generation tools at BoneSoft.com.
Hi,
BoneSoft wrote:
It does not also call Close(), only Dispose().
I am afraid you misunderstood me; you seem to think I expected the using statement to call both Close() and Dispose(). I know that is not the case. What I said was: "I did not find in MSDN doc a statement indicating that Dispose() also calls Close()". I am using the using statement quite a lot, and propagating it where ever I can. Regards
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
TheTinSoldier wrote:
if an object doesn't implement IDisposable is there any point in using Using?
If it doesn't implement IDisposable, you will get a compiler error if you try to use it in a
using
block.----------------------------- In just two days, tomorrow will be yesterday. http://geekswithblogs.net/sdorman
Good to know! Thanks!
-
It actually compiles to something like this:
public static bool IsPortInUse(string portName) {
try {
SerialPort sp = null;
try {
sp = new SerialPort(portName);
sp.Open();
return false;
} catch (Exception ex) {
throw ex;
} finally {
if (sp != null) {
sp.Dispose();
}
}
} catch (UnauthorizedAccessException e) {
return true;
}
}Which will automatically dispose, even if an exception occurs.
Try code model generation tools at BoneSoft.com.
That makes it clear! Thanks! :cool:
-
Hi Luc. In regards to your points: 1. I need to first open the port to call the IsOpen property. I am checking if the port is in use. (not by my application). 2. It would be nice to know if it does matter, as I'll do it your way if it's the right way 3. I get the feeling (from you and others) that even though the object will be disposed it wouldn't hurt to close it first. 4. This is interesting, I'd better check that out. I think for what I'm doing it should be ok. Thanks to you and everyone for their help!
Hi, 1. You need a SerialPort instance in order to get its IsOpen property; if you manage to open the port, you dont want IsOpen anymore since that now will always be true. 2. the one difference is whether the SerialPort constructor is in or outside the try; I would put it outside, since your method cannot cope with a failing constructor, if and when that happens, that exception really should go upstairs. But once you got the port, whatever you do to it is "internal affairs", and should be masked from the caller, hence we want that to be inside the try. 3. Assuming Dispose() calls Close() there is no need and no use in calling Close() explicitly. The Dispose() is immediate, the using statement is just a shorthand for a finally {port.Dispose()} as another poster already pointed out. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi, 1. You need a SerialPort instance in order to get its IsOpen property; if you manage to open the port, you dont want IsOpen anymore since that now will always be true. 2. the one difference is whether the SerialPort constructor is in or outside the try; I would put it outside, since your method cannot cope with a failing constructor, if and when that happens, that exception really should go upstairs. But once you got the port, whatever you do to it is "internal affairs", and should be masked from the caller, hence we want that to be inside the try. 3. Assuming Dispose() calls Close() there is no need and no use in calling Close() explicitly. The Dispose() is immediate, the using statement is just a shorthand for a finally {port.Dispose()} as another poster already pointed out. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
All good advice. Thanks for your time luc! :-D
-
It does not also call
Close()
, onlyDispose()
. But most things do callClose()
in their implementation ofDispose()
.
Try code model generation tools at BoneSoft.com.
Actually
Close
callsDispose
. Take a look in reflector:public class SerialPort : Component
{
public void Close()
{
this.Dispose(true);
}
protected override void Dispose(bool disposing)
{
if (disposing && this.IsOpen)
{
this.internalSerialStream.Flush();
this.internalSerialStream.Close();
this.internalSerialStream = null;
}
base.Dispose(disposing);
}
}
-
Actually
Close
callsDispose
. Take a look in reflector:public class SerialPort : Component
{
public void Close()
{
this.Dispose(true);
}
protected override void Dispose(bool disposing)
{
if (disposing && this.IsOpen)
{
this.internalSerialStream.Flush();
this.internalSerialStream.Close();
this.internalSerialStream = null;
}
base.Dispose(disposing);
}
}
Hi Ed, there must be a mistake somewhere; MSDN on SerialPort.Close explains: 1. Close() disposes of the internal streams (sounds OK) 2. you can Open() a port after it has been closed (cant be true if Close disposes of port) :confused:
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi Ed, there must be a mistake somewhere; MSDN on SerialPort.Close explains: 1. Close() disposes of the internal streams (sounds OK) 2. you can Open() a port after it has been closed (cant be true if Close disposes of port) :confused:
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }