/// <summary> /// Helper for returning a FileOperationState to be recycled. /// </summary> private void ReturnFileOperationState(FileOperationState state) { lock (pendingStates) { pendingStates.Enqueue(state); } }
/// <summary> /// Helper that performs our asynchronous GetFiles. /// </summary> private void DoGetFilesAsync(object asyncState) { FileOperationState state = asyncState as FileOperationState; Exception error = null; string[] result = null; // perform the GetFiles operation try { result = GetFiles(state.Container, state.Pattern); } catch (Exception e) { error = e; } // construct our event arguments GetFilesCompletedEventArgs args = new GetFilesCompletedEventArgs(error, result, state.UserState); // fire our completion event GetFilesCompleted(this, args); // recycle our state object ReturnFileOperationState(state); // decrement our pending operation count PendingOperationsDecrement(); }
/// <summary> /// Helper that performs our asynchronous deleting. /// </summary> private void DoDeleteAsync(object asyncState) { FileOperationState state = asyncState as FileOperationState; Exception error = null; // perform the delete operation try { Delete(state.Container, state.File); } catch (Exception e) { error = e; } // construct our event arguments FileActionCompletedEventArgs args = new FileActionCompletedEventArgs(error, state.UserState); // fire our completion event DeleteCompleted(this, args); // recycle our state object ReturnFileOperationState(state); // decrement our pending operation count PendingOperationsDecrement(); }
/// <summary> /// Helper that performs our asynchronous FileExists. /// </summary> private void DoFileExistsAsync(object asyncState) { FileOperationState state = asyncState as FileOperationState; Exception error = null; bool result = false; // perform the FileExists operation try { result = FileExists(state.Container, state.File); } catch (Exception e) { error = e; } // construct our event arguments FileExistsCompletedEventArgs args = new FileExistsCompletedEventArgs(error, result, state.UserState); // fire our completion event FileExistsCompleted(this, args); // recycle our state object ReturnFileOperationState(state); // decrement our pending operation count PendingOperationsDecrement(); }
/// <summary> /// Helper that performs our asynchronous loading. /// </summary> private void DoLoadAsync(object asyncState) { // set our processor affinity SetProcessorAffinity(); FileOperationState state = asyncState as FileOperationState; Exception error = null; // perform the load operation try { Load(state.Container, state.File, state.Action); } catch (Exception e) { error = e; } // construct our event arguments FileActionCompletedEventArgs args = new FileActionCompletedEventArgs(error, state.UserState); // fire our completion event if (LoadCompleted != null) { LoadCompleted(this, args); } // recycle our state object ReturnFileOperationState(state); // decrement our pending operation count PendingOperationsDecrement(); }
/// <summary> /// Helper for getting a FileOperationState object. /// </summary> private FileOperationState GetFileOperationState() { lock (pendingStates) { // recycle any states if we have some available if (pendingStates.Count > 0) { FileOperationState state = pendingStates.Dequeue(); state.Reset(); return(state); } return(new FileOperationState()); } }
/// <summary> /// Gets an array of all files available in a container that match the given pattern asynchronously. /// </summary> /// <param name="containerName">The name of the container in which to search for files.</param> /// <param name="pattern">A search pattern to use to find files.</param> /// <param name="userState">A state object used to identify the async operation.</param> public void GetFilesAsync(string containerName, string pattern, object userState) { // increment our pending operations count PendingOperationsIncrement(); // get a FileOperationState and fill it in FileOperationState state = GetFileOperationState(); state.Container = containerName; state.Pattern = pattern; state.UserState = userState; // queue up the work item ThreadPool.QueueUserWorkItem(DoGetFilesAsync, state); }
/// <summary> /// Determines if a given file exists asynchronously. /// </summary> /// <param name="containerName">The name of the container in which to check for the file.</param> /// <param name="fileName">The name of the file.</param> /// <param name="userState">A state object used to identify the async operation.</param> public void FileExistsAsync(String containerName, String fileName, object userState) { // increment our pending operations count PendingOperationsIncrement(); // get a FileOperationState and fill it in FileOperationState state = GetFileOperationState(); state.Container = containerName; state.File = fileName; state.UserState = userState; // queue up the work item ThreadPool.QueueUserWorkItem(DoFileExistsAsync, state); }
/// <summary> /// Deletes a file asynchronously. /// </summary> /// <param name="containerName">The name of the container from which to delete the file.</param> /// <param name="fileName">The file to delete.</param> /// <param name="userState">A state Object used to identify the async operation.</param> public void DeleteAsync(String containerName, String fileName, Object userState, FileLocationContainer container) { // increment our pending operations count PendingOperationsIncrement(); // get a FileOperationState and fill it in FileOperationState state = GetFileOperationState(); state.Container = containerName; state.File = String.Join("/", containerName, fileName); state.UserState = userState; state.Location = container; // queue up the work item ThreadPool.QueueUserWorkItem(DoDeleteAsync, state); }
/// <summary> /// Deletes a file asynchronously. /// </summary> /// <param name="containerName">The name of the container from which to delete the file.</param> /// <param name="fileName">The file to delete.</param> /// <param name="userState">A state object used to identify the async operation.</param> public void DeleteAsync(string containerName, string fileName, object userState) { if (DeleteStarted != null) { DeleteStarted(this, EventArgs.Empty); } // increment our pending operations count PendingOperationsIncrement(); // get a FileOperationState and fill it in FileOperationState state = GetFileOperationState(); state.Container = containerName; state.File = fileName; state.UserState = userState; // queue up the work item ThreadPool.QueueUserWorkItem(DoDeleteAsync, state); }
/// <summary> /// Helper for returning a FileOperationState to be recycled. /// </summary> private void ReturnFileOperationState(FileOperationState state) { lock (pendingStates) { pendingStates.Enqueue(state); } }