AsyncResult custom IAsyncResult class

Source: Internet
Author: User

Let's take a look at the custom IAsyncResult class of AsyncResult. I hope this article will help you.

The Code is as follows: Copy code

/// <Summary>
/// Asynchronous request object
/// </Summary>
Public class AsyncResult: IAsyncResult
{
Static readonly object lockObject = new object ();
Private ManualResetEvent completeEvent = null;

# Region IAsyncResult Member

/// <Summary>
/// Initialization
/// </Summary>
/// <Param name = "cb"> callback method </param>
/// <Param name = "asyncState"> User status data </param>
Internal AsyncResult (AsyncCallback cb, object asyncState)
{
This. Callback = cb;
This. AsyncState = asyncState;
This. IsCompleted = false;
This. ParentResult = null;
This. Exception = null;
This. State = null;
}

/// <Summary>
/// Obtain user status data
/// </Summary>
Public object AsyncState
{
Get;
Internal set;
}

/// <Summary>
/// Obtain or set custom status data
/// </Summary>
Public object State
{
Get;
Set;
}

/// <Summary>
/// Asynchronous wait handle
/// </Summary>
Public System. Threading. WaitHandle AsyncWaitHandle
{
Get
{
Lock (lockObject)
{
If (this. completeEvent = null)
This. completeEvent = new ManualResetEvent (false );

Return this. completeEvent;
}
}
}

/// <Summary>
/// Complete the preceding steps
/// </Summary>
Public bool CompletedSynchronously
{
Get {return false ;}
}

/// <Summary>
/// Completed
/// </Summary>
Public bool IsCompleted
{
Get;
Private set;
}

# Endregion


/// <Summary>
/// Parent Result. If yes
/// </Summary>
Public IAsyncResult ParentResult {get; set ;}

/// <Summary>
/// Obtain asynchronous call completion
/// </Summary>
Public AsyncCallback Callback
{
Get;
Internal set;
}


/// <Summary>
/// Obtain or set exceptions generated during the asynchronous Process
/// </Summary>
Public Exception {get; set ;}

/// <Summary>
/// Completes the request.
/// </Summary>
Public void Completed ()
{
This. IsCompleted = true;
Lock (lockObject)
{
If (this. completeEvent! = Null)
This. completeEvent. Set ();
}

If (this. Callback! = Null)
This. Callback (this );
}

/// <Summary>
/// Completes the request.
/// </Summary>
Public void Completed (Exception ex ){
This. Exception = ex;
This. Completed ();
}
}

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.