다음을 통해 공유


Async.AwaitEvent<'Del,'T> 메서드(F#)

CLI 이벤트에 처리기를 추가하여 해당 이벤트가 한 번 호출될 때까지 기다리는 비동기 계산을 만듭니다. 계산이 완료되거나 취소되면 처리기는 이벤트에서 제거됩니다.

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

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

// Signature:
static member AwaitEvent : IEvent<'Del,'T> * ?(unit -> unit) -> Async<'T> (requires delegate)

// Usage:
Async.AwaitEvent (event)
Async.AwaitEvent (event, cancelAction = cancelAction)

매개 변수

  • event
    형식: IEvent<'Del,'T>

    한 번 처리할 이벤트입니다.

  • cancelAction
    형식: (unit -> unit)

    취소가 발생하면 계산을 취소하는 대신 실행할 선택적 함수입니다.

반환 값

이벤트가 호출되기를 기다리는 비동기 계산입니다.

설명

계산은 이벤트를 기다리는 동안 취소에 응답합니다. cancelAction을 지정한 경우 취소가 발생하면 해당 작업이 실행되고 계산이 이벤트를 계속 기다립니다. cancelAction을 지정하지 않은 경우 취소가 발생하면 계산이 즉시 취소됩니다.

예제

다음 코드 예제에서는 Async.AwaitEvent를 사용하여 파일 작업을 설정하는 방법에 대해 보여줍니다. 이 때, 파일 작업은 파일이 변경되는 것을 나타내는 이벤트에 대한 응답으로 실행됩니다. 이 경우 이벤트를 기다리는 동안 잠긴 파일에 대해 액세스하려는 시도를 방지합니다.

open System.Windows.Forms
open System.IO

let filename = "longoutput.dat" 
if File.Exists(filename) then
    File.Delete(filename)
let watcher = new FileSystemWatcher(Path = Directory.GetCurrentDirectory(),
                                    NotifyFilter = NotifyFilters.LastWrite,
                                    Filter = filename)
watcher.Changed.Add(fun args -> printfn "The file %s is changed." args.Name)
watcher.EnableRaisingEvents <- true 

let testFile = File.CreateText("Test.txt")
testFile.WriteLine("Testing...")
testFile.Close()

let form = new Form(Text = "Test Form")
let buttonSpacing = 5
let button1 = new Button(Text = "Start")
let button2 = new Button(Text = "Start Invalid", Top = button1.Height + buttonSpacing)
let button3 = new Button(Text = "Cancel", Top = 2 * (button1.Height + buttonSpacing))
let label1 = new Label(Text = "", Width = 200, Top = 3 * (button1.Height + buttonSpacing))
let label2 = new Label(Text = "", Width = 200, Top = 4 * (button1.Height + buttonSpacing))
form.Controls.AddRange [| button1; button2; button3; label1 |]
form.Controls.Add(button1)

let bufferData = Array.zeroCreate<byte> 100000000

let async1 filename =
     async {
       printfn "Creating file %s." filename
       use outputFile = File.Create(filename)
       printfn "Attempting to write to file %s." filename
       do! outputFile.AsyncWrite(bufferData) 
     }

let async2 filename =
     async {
       printfn "Waiting for file system watcher notification." 
       // If you omit the call to AwaitEvent, an exception is thrown that indicates that the 
       // file is locked. 
       let! args = Async.AwaitEvent(watcher.Changed)
       printfn "Attempting to open and read file %s." filename
       use inputFile = File.OpenRead(filename)
       let! buffer = inputFile.AsyncRead(100000000)
       printfn "Successfully read file %s." filename
       return buffer
     }   

button1.Click.Add(fun _ ->
                  // Start these as tasks simultaneously.
                  Async.StartAsTask(async1 filename) |> ignore
                  Async.StartAsTask(async2 filename) |> ignore
                  ())
button2.Click.Add(fun _ ->
                  Async.StartAsTask(async1 filename) |> ignore
                  Async.StartAsTask(async2 "longoutputX.dat")  |> ignore
                  ())
Application.Run(form)

샘플 출력

  
  
  
  
  
  
  

플랫폼

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

버전 정보

F# 코어 라이브러리 버전

지원: 2.0, 4.0, 노트북

참고 항목

참조

Control.Async 클래스(F#)

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