public AsyncTask() { url = string.Empty; isText = false; receivedBytes = 0; totalBytes = 0; bufferRead = new byte[bufferSize]; requestData = new StringBuilder(); asyncTaskState = AsyncTaskState.None; exception = null; idleTime = 0f; retryTime = 0; }
// Private methods. /// <summary> /// Creates an asynchronous state, and executes the method on the thread pool. /// </summary> /// <param name="method">The method to execute.</param> /// <param name="userState">The user state.</param> private void Execute(AsyncTaskCallback method, object userState) { // Create the asynchronous state for the current method. AsyncState asyncState = new AsyncState(userState); // Execute the method on the thread pool. ThreadPool.QueueUserWorkItem((object state) => { // Lock the task. lock (this.syncTask) { // Lock the state. lock (this.syncState) { // Change the task state. this.taskState = AsyncTaskState.Running; // Set the current asychronous state. this.asyncState = asyncState; } // Execute the task method. method(asyncState); // If the method has been canceled. if (asyncState.IsCanceled) { // Execute the cancel callback method. asyncState.CancelCallback(); } // Lock the state. lock (this.syncState) { // Change the task state. this.taskState = AsyncTaskState.Ready; // Set the current asychronous state to null. this.asyncState = null; // Signal the completion of the asynchronous operation. asyncState.Complete(); } // Dispose of the asynchronous state. asyncState.Dispose(); } }); }