Async.SwitchToNewThread Yöntemi (F#)
Yeni bir iş parçacığı oluşturan ve bu iş parçacığında devamlılığını çalıştıran zaman uyumsuz bir hesaplama oluşturur.
Ad alanı/modül yolu: Microsoft.FSharp.Control
Derleme: FSharp.Core (FSharp.Core.dll),
// Signature:
static member SwitchToNewThread : unit -> Async<unit>
// Usage:
Async.SwitchToNewThread ()
Dönüş Değeri
Yeni bir iş parçacığında yürütülecek hesaplama.
Örnek
Aşağıdaki kod örneği nasıl kullanılacağını gösteren Async.SwitchToNewThread ve Async.SwitchToThreadPool zaman uyumsuz yöntem olarak eşzamanlı yöntem çağrısı kaydırmak için.
open System
open System.IO
let asyncMethod f =
async {
do! Async.SwitchToNewThread()
let result = f()
do! Async.SwitchToThreadPool()
return result
}
let GetFilesAsync(path) = asyncMethod (fun () -> Directory.GetFiles(path))
let listFiles path =
async {
let! files = GetFilesAsync(path)
Array.iter (fun elem -> printfn "%s" elem) files
}
printfn "Here we go..."
// The output is interleaved, which shows that these are both
// running simultaneously.
Async.Start(listFiles ".")
Async.Start(listFiles ".")
Console.WriteLine("Press a key to continue...")
Console.ReadLine() |> ignore
Platformlar
Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2
Sürüm Bilgisi
F# Çekirdek Kitaplığı sürümleri
Desteklenen: 2.0, 4.0, Portable