[Asynchandle] What triggered the objectdisposedexception?

Source: Internet
Author: User
Tags dotnet garbage collection

Version

Date

Creator

Description

1.0.0.1

2006-9-19

Zheng

Draft

Before continuing, let's assume you are familiar with the following knowledge:

n releasing dotnet Resources with IDisposable interface

If you are unfamiliar with these knowledge points, you can look at the article in the last resource list of this article.

This article discusses the occurrence of an objectdisposedexception exception crash, looking for possible causes and trying to resolve it. [Phenomenon]

key Words : objectdisposedexception .

appearance : My dotnet C # service breaks down occasionally, with no regularity to follow, and crashes in the Windows event log from " . NET Runtime 2.0 Error Reporting

Error description :

Event Type: Error

Event Source:. NET Runtime 2.0 Error Reporting

Event Type: No

Event id:5000

Description:

eventtype Clr20r3, P1 juiceextractor.exe, P2 1.0.0.0, P3 450d5160, P4 mscorlib, P5 2.0.0.0, P6 4333ab80, P7 10ed, P8 0, P9 system.objectdisposedexception, P10 NIL.

By ObjectDisposedException to our reminders, you can learn that this is because a disposed object has been used before. Invoking other methods on a disposed object can cause a fatal exception, and the service does not know where to catch the exception, nor does it seem to be captured. But the question is, who triggered it.

extension : See Microsoft's ServicePack Bulletin of the dotnet framework mentions that when an asynchronous Web request is aborted before receiving a response, it is raised objectdisposedexcept Ion.

The only thing that has something to do with asynchronous requests is that the service sends a request to a socket server and waits for a period of time to loop through a large amount of data feedback from the receiving server. In the middle, C # asynchronous method BeginReceive is used to tell the socket how to receive the data.

The code for this logic is as follows:

C # code

///<summary>

/// calling Asynchronous Methods BeginReceive to tell Socket how to receive data

///</summary>

IAsyncResult ar =

_socket. BeginReceive (_recvbuffer.buffer, 0, _recvbuffer.buffer.length,

Socketflags.none, _recvcallback, _socket);

 

// set a deadline, Within ten minutes if there is no result returned,

// we can think of the need to turn off the current Socket It 's connected, don't wait any longer. !

ar. Asyncwaithandle.waitone (New TimeSpan (0, 0), true);

ar. Asyncwaithandle.close ();

... ..

Public void Recvcallback (IAsyncResult result)

{... ..

That is, use the callback function " _recvcallback ", every time there is data, call it directly. However, the asynchronous wait handle is closed if no full results are returned within ten minutes.

Very simple idea.

But if the data volume is large, the socket server calculation process is very long, resulting in more than 10 minutes to return the data, what will happen. [Answer the previous question]

This time, " ar. Asyncwaithandle.close (); "Is it the cause of objectdisposedexception?"

Suppose so.

First, do we need to call Asyncwaithandle.close () this sentence.

The Thread Pool and asynchronous Methods said: "Note the called to AR." Asyncwaithandle.close (). This prevents the WaitHandle leaking until garbage collection. The leak wouldn ' t cause any problems in most cases (unlike, for instance, file handles leaking), but in situations where F Ireandforget would is called many, many times in quick succession, and you could end up with a vast number of handles th E garbage collector started finalizing them. (This is also-a bad thing in terms of performance-you shouldn ' t leave things to be finalised as it can be avoided.) ”

Well, maybe we can not call it to circumvent the ObjectDisposedException exception crash.

Second, put ar. Asyncwaithandle.waitone (New TimeSpan (0, 0), true); The waiting time is prolonged. For example, extend to 40 minutes. This can also avoid objectdisposedexception. [Change reception mode]

Third, for such a long time to execute the socket server, you can not wait on a port, you can send a request with a port, receive data feedback with a different socket port number.

The

did not take into account that the calculation time is so long before the socket asynchronous receive mode is made. Then the algorithm changed, and sometimes the 10-minute wait was not enough.   [Resources]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.