T
Trecius
Hello, Newsgroupians:
Here's my situation. I have created a class that connects to another
device. It reads and sends information using a stream. The device it
connects to is proprietary and has its own set of commands. When I connect,
I can parse various properties of the system. However, when I am done, I
send it the "&EXIT." This tells the machine that I am disconnecting.
Now, I like ordered code just as much as the next person, so I try to create
my code with fault tolerances built into it. Therefore, if another
individual using my code forgets to call, Disconnect() which sends the
"&EXIT" message, I want to send that before the object is garbage collected.
This is synonymous to implementing a Dipose() method, and if the person
forgets to Dispose() the object, the destructor will do it for you.
However, I am running into problems. I purposefully forget to call
Disconnect(), so my destructor is calling my Disconnect() function. However,
my stream has already been marked and called for garbage collection. What
should I do?
An abbreviated example of my code is as follows...
~MyClass()
{
if (this.m_stream != null)
{
this.Disconnect();
}
}
public void Disconnect()
{
if (this.m_stream != null)
{
this.Write("&EXIT");
this.m_stream.Close();
this.m_stream = null;
}
}
Again, if someone hasn't called Disconnect(), the destructor should call
Disconnect(), but by that time the stream has already been called for garbage
collection. Maybe I should do GC.SuppressFinalize()? Thank you, all.
Trecius
Here's my situation. I have created a class that connects to another
device. It reads and sends information using a stream. The device it
connects to is proprietary and has its own set of commands. When I connect,
I can parse various properties of the system. However, when I am done, I
send it the "&EXIT." This tells the machine that I am disconnecting.
Now, I like ordered code just as much as the next person, so I try to create
my code with fault tolerances built into it. Therefore, if another
individual using my code forgets to call, Disconnect() which sends the
"&EXIT" message, I want to send that before the object is garbage collected.
This is synonymous to implementing a Dipose() method, and if the person
forgets to Dispose() the object, the destructor will do it for you.
However, I am running into problems. I purposefully forget to call
Disconnect(), so my destructor is calling my Disconnect() function. However,
my stream has already been marked and called for garbage collection. What
should I do?
An abbreviated example of my code is as follows...
~MyClass()
{
if (this.m_stream != null)
{
this.Disconnect();
}
}
public void Disconnect()
{
if (this.m_stream != null)
{
this.Write("&EXIT");
this.m_stream.Close();
this.m_stream = null;
}
}
Again, if someone hasn't called Disconnect(), the destructor should call
Disconnect(), but by that time the stream has already been called for garbage
collection. Maybe I should do GC.SuppressFinalize()? Thank you, all.
Trecius