Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# cannot access a disposed object

I am making an server/client application.

I set server socket to listening, and set BeginAccept() method. And when I closed server socket (Socket.Close()) to turn off server, an exception thrown from BeginAccept() method's async callback method. I inspected exception, and I found the exception saying me this:

Message "Cannot access a disposed object named "System.Net.Sockets.Socket". Object name: "System.Net.Sockets.Socket"." String

In my opinion, this is just this: "The socket disposed when I call Socket.Close() method, but the callback did not released before the socket closed."

i did search on the net and found this is not an error but a designed exception as beginaccept was cancelled.

my question how do i handle this excepton? what processing there needs to be for it?

I'm just going to treat it as a normal event:

      OnNetworkEvents eventArgs = new OnNetworkEvents(false, "Ready", e.Message);
      OnUpdateNetworkStatusMessage(this, eventArgs);

any comments are still welcome.

like image 888
iTEgg Avatar asked Jan 09 '10 19:01

iTEgg


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

If you don't care about the exception (i.e. it's not an "exception", it's the normal operating procedure and happens every time), you can just ignore/suppress it. (Just as long as it won't happen in a situation that indicates a real error)

If you want to suppress it more tidily, then you can set a flag that tells your callback that it should shut down, and have the callback actually do the closing/disposing of the socket. That way you can guarantee that there are no pending reads on the socket and won't get the exception (but this means it will hang about indefinitely, until another packet is received - which of course may never happen).

If you are in control of both ends of the communications link, then you can send an explicit "shutdown" message that tells the client that the connection should be closed (and this can of course be applied in the callback when the message is received).

(But I'd love to hear if anyone else has found a better solution as I have a similar situation with a UDP comms implementation)

like image 187
Jason Williams Avatar answered Sep 22 '22 14:09

Jason Williams