다음을 통해 공유


Async.OnCancel 메서드(F#)

업데이트: 2010년 7월

비동기 워크플로 내에서 사용할 수 있도록 범위가 지정된 협조적 취소 처리기를 생성합니다.

네임스페이스/모듈 경로: Microsoft.FSharp.Control

어셈블리: FSharp.Core(FSharp.Core.dll)

// Signature:
static member OnCancel : (unit -> unit) -> Async<IDisposable>

// Usage:
Async.OnCancel (interruption)

매개 변수

  • interruption
    형식: unit -> unit

    취소를 수행하는 스레드에서 실행되는 함수입니다.

반환 값

삭제되기 전에 취소된 경우 중단을 트리거하는 비동기 계산입니다.

설명

예를 들어 다음 코드는 holder의 범위에서 비동기 계산을 실행하는 동안 언제든지 취소가 발생하면 취소를 수행하고 있는 스레드에서 작업 interruption이 실행되도록 하는 비동기 계산을 생성합니다. 기 기능은 플래그를 설정하거나 보류 중인 I/O 작업을 등록 취소하는 등 취소가 발생할 경우 이를 비동기적으로 알리기 위해 계산을 정렬하는 데 사용됩니다.

async { use! holder = Async.OnCancel interruption ... }

예제

다음 코드 예제에서는 Async.OnCancel를 사용하는 방법을 보여 줍니다.

// This is a simulated cancellable computation. It checks the token source
// to see whether the cancel signal was received.
let computation (tokenSource:System.Threading.CancellationTokenSource) =
    async {
        use! cancelHandler = Async.OnCancel(fun () -> printfn "Canceling operation.")
        // Async.Sleep checks for cancellation at the end of the sleep interval,
        // so loop over many short sleep intervals instead of sleeping
        // for a long time.
        while true do
            do! Async.Sleep(100)
    }

let tokenSource1 = new System.Threading.CancellationTokenSource()
let tokenSource2 = new System.Threading.CancellationTokenSource()

Async.Start(computation tokenSource1, tokenSource1.Token)
Async.Start(computation tokenSource2, tokenSource2.Token)
printfn "Started computations."
System.Threading.Thread.Sleep(1000)
printfn "Sending cancellation signal."
tokenSource1.Cancel()
tokenSource2.Cancel()

// Wait for user input to prevent application termination.
System.Console.ReadLine() |> ignore

Output

          

플랫폼

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

버전 정보

F# 런타임

지원되는 버전: 2.0, 4.0

Silverlight

지원되는 버전: 3

참고 항목

참조

Control.Async 클래스(F#)

Microsoft.FSharp.Control 네임스페이스(F#)

변경 기록

날짜

변경 내용

이유

2010년 7월

코드 예제를 추가했습니다.

향상된 기능 관련 정보