Hi Guys,
Im having trouble sending a final command to a service before my class closes.
I have a startupclient that starts when my application starts and attempts to send a "QUIT" message from the class destructor as the class is closing.
Unfortunately when calling SendCommand("QUIT") from the class destructor, I get an exception from the SendCommand method.
System.ObjectDisposedException was unhandled
HResult=-2146232798
Message=Cannot access a disposed object.
Object name: 'System.Net.Sockets.TcpClient'.
ObjectName=System.Net.Sockets.TcpClient
Source=System
StackTrace:
at System.Net.Sockets.TcpClient.GetStream()
at Skeleton.Classes.clsStartupClient.SendCommand(String Command, String Param)
at Skeleton.Classes.clsStartupClient.SendCommand(String Command)
at Skeleton.Classes.clsStartupClient.Finalize()
InnerException:
on the line below in the SendCommand() method.
StreamWriter wtr = new StreamWriter(Cli.GetStream());
How would I be able to send a final SendCommand message before the class closes?
Thanks in advance.
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Donald CPosted May 18, 2016, 3:38 PM
Thanks for your reply.
You're right, I do believe the class object has been closed \ destructed by the time I try to send the command.
Unfortunately I cant use the Using \ Try catch options because I open the TcpClient in a class constructor when the application starts and "try" to close it in the destructor of that class..
Bhuvanesh MohankumarPosted May 5, 2016, 4:19 AM
This is because you are trying to access a class object which was already destructed/disposed.
Before you trigger the dispose, try to have a catch block where don't handle the exception rather just throw the error using keyword "throw"and then youtrigger for the dispose of object.