Table of contents
TOC
Collapse the table of content
Expand the table of content

Async.StartAsTask<'T> Method (F#)

Den Delimarsky|Last Updated: 6/10/2016
|
1 Contributor

Executes a computation in the thread pool. Returns a System.Threading.Tasks.Task that will be completed in the corresponding state once the computation terminates (produces the result, throws exception or gets canceled) If no cancellation token is provided then the default cancellation token is used.

Namespace/Module Path: Microsoft.FSharp.Control

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

Syntax

// Signature:
static member StartAsTask : Async<'T> * ?TaskCreationOptions * ?CancellationToken -> Task<'T>

// Usage:
Async.StartAsTask (computation)
Async.StartAsTask (computation, taskCreationOptions = taskCreationOptions, cancellationToken = cancellationToken)

Parameters

computation Type: Async<'T>

The computation to execute.

taskCreationOptions Type: System.Threading.Tasks.TaskCreationOptions

Optional task creation options.

cancellationToken Type: System.Threading.CancellationToken

Optional cancellation token.

Return Value

Returns a System.Threading.Tasks.Task<'T> object that represents the given computation.

Example

The following code example demonstrates the use of Async.StartAsTask.


    open System.Windows.Forms

    let bufferData = Array.zeroCreate<byte> 100000000

    let async1 =
         async {
           use outputFile = System.IO.File.Create("longoutput.dat")
           do! outputFile.AsyncWrite(bufferData) 
         }
      

    let form = new Form(Text = "Test Form")
    let button = new Button(Text = "Start")
    form.Controls.Add(button)
    button.Click.Add(fun args -> let task = Async.StartAsTask(async1)
                                 printfn "Do some other work..."
                                 task.Wait()
                                 printfn "done")
    Application.Run(form)

Platforms

Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 4.0, Portable

See Also

Control.Async Class (F#)

Microsoft.FSharp.Control Namespace (F#)

© 2016 Microsoft