WHERE and WHEN the SerialPort been closed ?

N

Nie longhai

Hi, all

I'm newbie in .net. I use SerialPort to control Hardware.

below is my test code:

===========================
namespace WindowsApplication6
{
public partial class Form1 : Form
{
MyPort m_port = new MyPort();

public Form1()
{
}

protected override void OnClosing(CancelEventArgs e)
{
MessageBox.Show("OnClosing: Port status=" + m_port.IsOpen);
base.OnClosing(e);
}
}

public class MyPort : SerialPort
{
public MyPort()
{
PortName = "COM1";
Open();
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
MessageBox.Show("Dispose: Port status=" + IsOpen);
}

~MyPort()
{
MessageBox.Show("~MyCOM: Port status=" + IsOpen);
}
}
}

=======================
Debug Run. Close Form...

in Form's Closing(). the Port is Opened,
BUT in MyPort's Dispose() and ~MyPort(). the port is been CLOSED !!!!!!!,
Where and When the port been closed? It seems the GC close the port ?

I need send 'LogOut' Command before Port closed.
so How can I do ?

Can anyone give me somg advice ?

thanks
--
 
G

Gaurav Vaish \(www.EdujiniOnline.com\)

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
MessageBox.Show("Dispose: Port status=" + IsOpen);
}

Port has been closed in "base.Dispose(disposing)" with "disposing" of
"true", which is called by the method "Dispose()" in Component of which
SerialPort is a sub-class.

Disposing is done for all "Component"-s associated with the "Form" once it
goes for "Closing".


--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujinionline.com
http://eduzine.edujinionline.com
-----------------------------------------
 
L

Lau Lei Cheong

On Dispose and destructor, the managed resources would have been freed so
the ports opened by the component should have been closed.

I'll suggest you to override the Close() method and send the "Logout"
command before base.Close().
 
N

Nie longhai

Hi, Lau

thank you for reply,

I want 'Logout' command sended in override Dispose().
Are there any method make me send 'Logout' command before port closed by GC
?



Lau Lei Cheong said:
On Dispose and destructor, the managed resources would have been freed so
the ports opened by the component should have been closed.

I'll suggest you to override the Close() method and send the "Logout"
command before base.Close().
 
L

Lau Lei Cheong

I'm afraid not. By the time Dispose event is fired, the port should have
already closed.

You can't send anything unless you open the port again, but that'll then
defeat the purpose.

By the way, I believe Close() is called whenever the connection is manually
closed, so it'll be appropiate. (If the connection is unexpectedly
disconnected, you'll never have the chance to send LogOut anyway...)

Nie longhai said:
Hi, Lau

thank you for reply,

I want 'Logout' command sended in override Dispose().
Are there any method make me send 'Logout' command before port closed by
GC ?
 
C

cok119

Hi, Lau

It seem's that I have to send 'Logout' command in override Dispose(),
and Dispose() port manually.

thank you very much
 
C

cok119

Hi , Gaurav

I found the reason:

Use reflactor, I found SerialPort class contain a 'SerialStream' member,
SerialStream is a win32 COM wapper.

when execute to MyPort::Dispose(bool)
the SerialStream is ALREADY disposed, and COM port is closed in
SerialStream::Dispose() method.

so at this time, IsOpen return false;

thank you for your reply

"Gaurav Vaish (www.EdujiniOnline.com)"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top