|
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.
|
|
|
|
|
That makes it clear! Thanks!
|
|
|
|
|
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).
|
|
|
|
|
It does not also call Close() , only Dispose() . But most things do call Close() in their implementation of Dispose() .
|
|
|
|
|
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
|
|
|
|
|
Actually Close calls Dispose . 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)
|
|
|
|
|
Luc Pattyn wrote: you can Open() a port after it has been closed (cant be true if Close disposes of port)
Looking at the code more deeply all that the base.Dispose(true) does is remove the component from the container and fires the Disposed event. So looks like it may remain usable.
|
|
|
|
|
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.
|
|
|
|
|
All good advice. Thanks for your time luc!
|
|
|
|
|
Hello,
When i use the Fill() method from the data adapter, every column from the sql table is being mapped to a c# type variable,now i need the original sql server type of each column to dynamically populate a sqlCommand's parameters. How can I map the variable back to it's corresponding SqlDbType, is it possible?
example:
table customers(integers int,
MyFloats floats)
...........
select * from customers -> myCusomers
myCustomer.Columns[0].DataType.Name=Int32
how can i specify that my Int32 was a SqlDbType.Int
desired output is:
cmd.Parameters.Add("@ints", [myCustomer.Columns[0] as SqlDbType.Int], [myCustomer.Columns[0].size], myCustomer.Columns[0].Name);
Thanks!
drinking beer is fun
|
|
|
|
|
The simplest way would be to implement a simple look up table:
internal static class SqlHelper
{
private static Dictionary<Type, SqlDbType> types = new Dictionary<Type, SqlDbType>();
static SqlHelper()
{
types.Add(typeof(int), SqlDbType.Int);
types.Add(typeof(double), SqlDbType.Float);
}
public static SqlDbType GetType(Type type)
{
if (types.ContainsKey(type))
return types[type];
return SqlDbType.Variant;
}
}
|
|
|
|
|
Thank you Ed, your information was very helpful and elegant
drinking beer is fun
|
|
|
|
|
Im am developing a windows app, and i have data populated into a gridview. Instead of allowing users to edit the data in the grid(which is probably the easiest way) I want to be able to select a row and have a modal form pop up with Textboxes and other controls to edit the data. How would i go about populating data in those controls from with the information of with the gridview?
|
|
|
|
|
Add a Column/Link button to the row, handle the cell content click event and pass a reference to the row data to the new form:
<code>private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv.Columns[e.ColumnIndex] is DataGridViewButtonColumn)
{
DataForm df = new DataForm(dgv.Rows[e.RowIndex]);
df.ShowDialog();
}
}
I think that should do it!
-- modified at 17:33 Wednesday 18th July, 2007
|
|
|
|
|
hi all
i need an API that enables my application to send and recieve e-mails using SMTP & POP3
p.s. dont want these mails to be recieved as spams
Mostafa Ismail
|
|
|
|
|
You can use the System.Net.Mail namespace to send emails. Receiving them isn't built into the .NET Framework. You'll have to use some other library or P/Invoke the MAPI Win32 functions to get the mail. Or, be adventurous and write your own POP3 client.
Mostafa_Ismail wrote: .s. dont want these mails to be recieved as spams
You don't get to make that determination. Whether or not your email is received and interpreted as spam is entirely up to the person receiving it.
|
|
|
|
|
|
Is it possible to make a button lose it's focus once a mouse has passed it? (i mean, if it's clicked and held down and then the mouse moves out of the button).
for example, the buttons in Minesweeper are like that. so, is there any way to do so?
|
|
|
|
|
Set the focus to something else.
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
"I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless."
My website
|
|
|
|
|
but how would i know which button to set the focus to? (did you even look at minesweeper to see what i meant?)
|
|
|
|
|
sharpiesharpie wrote: but how would i know which button to set the focus to?
Don't set focus to another button. Set it to a panel, or a label, or anything else. Panels and labels are good because they don't show as focused.
sharpiesharpie wrote: (did you even look at minesweeper to see what i meant?)
Wow! Way to get pissy.
|
|
|
|
|
In Minewseeper they might look and act like buttons, but there's clearly some additional functionality built in, not to mention the actual Minesweeper "grid" itself.
To emulate that, I think you're going to have to spend a bit of time developing your own custom control - although this will clearly take a bit more time, it will afford you much finer control (pun intended )
"It was the day before today.... I remember it like it was yesterday."
-Moleman
|
|
|
|
|
Well...i did make a custom control, a panel that has an array of rectangles in it into which i render images, but it was EXTREMELY flickery for some reason (even though i used a backbuffer) :/
|
|
|
|