Share via


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

Starts a child computation within an asynchronous workflow. This allows multiple asynchronous computations to be executed simultaneously.

Namespace/Module Path: Microsoft.FSharp.Control

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

// Signature:
static member StartChild : Async<'T> * ?int -> Async<Async<'T>>

// Usage:
Async.StartChild (computation)
Async.StartChild (computation, millisecondsTimeout = millisecondsTimeout)

Parameters

  • computation
    Type: Async<'T>

    The child computation.

  • millisecondsTimeout
    Type: int

    The timeout value in milliseconds. If one is not provided then the default value is -1, which corresponds to Infinite.

Return Value

A new computation that waits for the input computation to finish.

Remarks

This method should normally be used as the immediate right-hand-side of a let! binding in an F# asynchronous workflow, that is:

 async { 
    ...
    let! completor1 = childComputation1
    |> Async.StartChild
    let! completor2 = childComputation2
    |> Async.StartChild
    ... 
    let! result1 = completor1
    let! result2 = completor2
     ... }

When used in this way, each use of StartChild starts an instance of childComputation and returns a completor object representing a computation to wait for the completion of the operation. When executed, the completor awaits the completion of childComputation.

Example

The following code example illustrates the use of Async.StartChild.

open System.Windows.Forms

let bufferData = Array.zeroCreate<byte> 100000000

let asyncChild filename =
        async {
            printfn "Child job start: %s" filename
            use outputFile = System.IO.File.Create(filename)
            do! outputFile.AsyncWrite(bufferData)
            printfn "Child job end: %s " filename
        }

let asyncParent =
        async {
            printfn "Parent job start."
            let! childAsync1 = Async.StartChild(asyncChild "longoutput1.dat")
            let! childAsync2 = Async.StartChild(asyncChild "longoutput2.dat")
            let! result1 = childAsync1
            let! result2 = childAsync2
            printfn "Parent job end."
        }


let form = new Form(Text = "Test Form")
let button = new Button(Text = "Start")
form.Controls.Add(button)
button.Click.Add(fun args -> Async.Start(asyncParent)
                             printfn "Completed execution." )
Application.Run(form)

Sample Output

The output is interleaved because the jobs are running simultaneously.

ComplParent job start.
eted execution.
Child job start: Child job slongoutput1.dat
tart: longoutput2.dat
Child job end: longoutput2.dat
Child job end: longoutput1.dat
Parent job end.

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#)