Bagikan melalui


Async.AsBeginEnd<'Arg,'T> Method (F#)

Creates three functions that can be used to implement the .NET Framework Asynchronous Programming Model (APM) for the supplied asynchronous computation.

Namespace/Module Path: Microsoft.FSharp.Control

Assembly: FSharp.Core (in FSharp.Core.dll)

// Signature:
static member AsBeginEnd : ('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)

// Usage:
Async.AsBeginEnd (computation)

Parameters

  • computation
    Type: 'Arg -> Async<'T>

    A function that generates the asynchronous computation to split into the traditional .NET Framework Asynchronous Programming Model.

Return Value

A tuple of the begin, end, and cancel members.

Remarks

For more information about the .NET Framework Asynchronous Programming Model, see Asynchronous Programming Overview.

The begin, end, and cancel functions should normally be published as members that are prefixed with Begin, End, and Cancel, and that can be used within a type definition as follows.

 let beginAction,endAction,cancelAction =
     Async.AsBeginEnd (fun arg -> computation)
 member x.BeginSomeOperation(arg, callback ,state:obj) =
     beginAction(arg, callback, state)
 member x.EndSomeOperation(iar) = endAction(iar)
 member x.CancelSomeOperation(iar) = cancelAction(iar)

If the asynchronous computation takes no arguments, AsBeginEnd is used as follows.

 let beginAction,endAction,cancelAction =
     Async.AsBeginEnd (fun () -> computation)
 member x.BeginSomeOperation(callback, state:obj) =
     beginAction((), callback, state)
 member x.EndSomeOperation(iar) = endAction(iar)
 member x.CancelSomeOperation(iar) = cancelAction(iar)

If the asynchronous computation takes two arguments, AsBeginEnd is used as follows.

 let beginAction,endAction,cancelAction =
     Async.AsBeginEnd (fun arg1 arg2 -> computation)
 member x.BeginSomeOperation(arg1, arg2, callback, state:obj) =
     beginAction((), callback, state)
 member x.EndSomeOperation(iar) = endAction(iar)
 member x.CancelSomeOperation(iar) = cancelAction(iar)

In each case, the resulting API resembles that used in other .NET Framework languages and is a useful way to publish asynchronous computations in components that are intended to be used from other .NET Framework languages.

Platforms

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Control.Async Class (F#)

Microsoft.FSharp.Control Namespace (F#)

AsyncCallback

IAsyncResult