|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
WHERE and WHEN the SerialPort been closed ?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 -- > protected override void Dispose(bool disposing) Port has been closed in "base.Dispose(disposing)" with "disposing" of > { > base.Dispose(disposing); > MessageBox.Show("Dispose: Port status=" + IsOpen); > } "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 ----------------------------------------- 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)" <gaurav.vaish.nospam@nospam.gmail.com> дÈëÏûÏ¢ÐÂÎÅ:Opy%238YH7GHA.3***@TK2MSFTNGP02.phx.gbl... Show quote >> 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 > ----------------------------------------- > > 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(). Show quote "Nie longhai" <cok***@163.com> ¼¶¼g©ó¶l¥ó·s»D:%231MScRA7GHA.2***@TK2MSFTNGP04.phx.gbl... > 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 > -- > > > 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 ? Show quote "Lau Lei Cheong" <leu***@yehoo.com.hk> дÈëÏûÏ¢ÐÂÎÅ:OKZPRdC7GHA.4***@TK2MSFTNGP02.phx.gbl... > 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(). > > "Nie longhai" <cok***@163.com> ¼¶¼g©ó¶l¥ó·s»D:%231MScRA7GHA.2***@TK2MSFTNGP04.phx.gbl... >> 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 >> -- >> >> >> > > 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...) Show quote "Nie longhai" <cok***@163.com> ¼¶¼g©ó¶l¥ó·s»D:uKZ$2oC7GHA.4***@TK2MSFTNGP05.phx.gbl... > 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" <leu***@yehoo.com.hk> дÈëÏûÏ¢ÐÂÎÅ:OKZPRdC7GHA.4***@TK2MSFTNGP02.phx.gbl... >> 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(). >> >> "Nie longhai" <cok***@163.com> ¼¶¼g©ó¶l¥ó·s»D:%231MScRA7GHA.2***@TK2MSFTNGP04.phx.gbl... >>> 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 >>> -- >>> >>> >>> >> >> > > Hi, Lau
It seem's that I have to send 'Logout' command in override Dispose(), and Dispose() port manually. thank you very much Show quote "Lau Lei Cheong" <leu***@yehoo.com.hk> дÈëÏûÏ¢ÐÂÎÅ:OnaZ5WD7GHA.2***@TK2MSFTNGP05.phx.gbl... > 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" <cok***@163.com> ¼¶¼g©ó¶l¥ó·s»D:uKZ$2oC7GHA.4***@TK2MSFTNGP05.phx.gbl... >> 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" <leu***@yehoo.com.hk> дÈëÏûÏ¢ÐÂÎÅ:OKZPRdC7GHA.4***@TK2MSFTNGP02.phx.gbl... >>> 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(). >>> >>> "Nie longhai" <cok***@163.com> ¼¶¼g©ó¶l¥ó·s»D:%231MScRA7GHA.2***@TK2MSFTNGP04.phx.gbl... >>>> 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 >>>> -- >>>> >>>> >>>> >>> >>> >> >> > > |
|||||||||||||||||||||||