共用方式為


Async.AwaitEvent<'Del,'T> 方法 (F#)

更新:2010 年 7 月

建立非同步計算,該計算會透過將處理常式加入至事件,等待單一叫用 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 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#)

變更記錄

日期

History

原因

2010 年 7 月

加入程式碼範例。

資訊加強。