// (c) Microsoft Corporation 2005-2009. namespace Microsoft.FSharp.Control open System open Microsoft.FSharp.Control /// Represents the reified result of an asynchronous computation type AsyncResult<'T> = | AsyncOk of 'T | AsyncException of exn | AsyncCanceled of OperationCanceledException /// Return an asynchronous computation that, when run, either returns a value, raises an exception /// of cancels according to the value of the asynchronous result. static member Commit : AsyncResult<'T> -> Async<'T> [] /// A helper type to store a single result from an asynchronous computation and asynchronously /// access its result. /// /// When using .NET 4.0 you can often use Task<'T> instead of this type type AsyncResultCell<'T> = /// Record the result in the AsyncResultCell. /// Subsequent sets of the result are ignored. This can happen, /// e.g. for a race between a cancellation and a success. member RegisterResult:AsyncResult<'T> -> unit /// Wait for the result and commit it member AsyncResult : Async<'T> /// Create a new result cell new : unit -> AsyncResultCell<'T> [] /// Extensions to the F# Microsoft.FSharp.Control.Async module module AsyncExtensions = #if FX_NO_CREATE_DELEGATE #else type Microsoft.FSharp.Control.Async<'T> with /// Return an asynchronous computation that waits for a single invocation of a .NET event by /// internally adding a handler to the event. Once the computation completes or is cancelled, the /// handler is removed from the event. /// /// The computation will respond to cancellation while waiting for the completion /// of the operation. If 'cancelAction' is specified, cancellation causes the given /// function to be executed. The computation then continues to wait for the event. /// /// If 'cancelAction' is not specified, then cancellation causes the computation /// to stop immediately, and if the event is subsequently raised it may /// be ignored. static member AwaitEvent: IEvent<'Delegate,'T> * ?cancelAction : (unit -> unit) -> Async<'T> when 'Delegate : delegate<'T,unit> and 'Delegate :> System.Delegate #endif type Microsoft.FSharp.Control.Async<'T> with /// Return three functions that can be used to implement the .NET Asynchronous /// Programming Model (APM) for a given asynchronous computation. /// /// The functions should normally be published as members with prefix 'Begin', /// 'End' and 'Cancel', and can be used within a type definition as follows: /// /// let beginAction,endAction,cancelAction = Async.AsBeginEnd computation /// member x.BeginSomeOperation(callback,state) = beginAction(callback,state) /// member x.EndSomeOperation(iar) = endAction(iar) /// member x.CancelSomeOperation(iar) = cancelAction(iar) /// /// /// The resulting API will be familiar to programmers in other .NET languages and /// is a useful way to publish asynchronous computations in .NET components. static member AsBeginEnd : computation:Async<'T> -> // The 'Begin' member (System.AsyncCallback * obj -> System.IAsyncResult) * // The 'End' member (System.IAsyncResult -> 'T) * // The 'Cancel' member (System.IAsyncResult -> unit) [] module StreamReaderExtensions = open System.IO type System.IO.StreamReader with member AsyncReadToEnd: unit -> Async [] member ReadToEndAsync : unit -> Async [] module FileExtensions = open System.IO type System.IO.File with /// Return an asynchronous computation that will open the file via a fresh blocking I/O thread. static member AsyncOpenText: path:string -> Async /// Return an asynchronous computation that will open the file via a fresh blocking I/O thread. static member AsyncOpenRead: path:string -> Async /// Return an asynchronous computation that will open the file via a fresh blocking I/O thread. static member AsyncOpenWrite: path:string -> Async /// Return an asynchronous computation that will open the file via a fresh blocking I/O thread. static member AsyncAppendText: path:string -> Async /// Return an asynchronous computation that will open the file via a fresh blocking I/O thread. /// Pass 'options=FileOptions.Asynchronous' to enable further asynchronous read/write operations /// on the FileStream. #if FX_NO_FILE_OPTIONS static member AsyncOpen: path:string * mode:FileMode * ?access: FileAccess * ?share: FileShare * ?bufferSize: int -> Async #else static member AsyncOpen: path:string * mode:FileMode * ?access: FileAccess * ?share: FileShare * ?bufferSize: int * ?options: FileOptions -> Async #endif [] static member OpenTextAsync: path:string -> Async [] static member OpenReadAsync: path:string -> Async [] static member OpenWriteAsync: path:string -> Async [] static member AppendTextAsync: path:string -> Async [] #if FX_NO_FILE_OPTIONS static member OpenAsync: path:string * mode:FileMode * ?access: FileAccess * ?share: FileShare * ?bufferSize: int -> Async #else static member OpenAsync: path:string * mode:FileMode * ?access: FileAccess * ?share: FileShare * ?bufferSize: int * ?options: FileOptions -> Async #endif #if FX_NO_WEB_REQUESTS #else [] module WebRequestExtensions = type System.Net.WebRequest with /// Return an asynchronous computation that, when run, will wait for a response to the given WebRequest. member AsyncGetResponse : unit -> Async [] member GetResponseAsync : unit -> Async #endif #if FX_NO_WEB_CLIENT #else [] module WebClientExtensions = type System.Net.WebClient with member AsyncDownloadString : address:System.Uri -> Async #endif