Share via


MailboxProcessor.Start<'Msg> 메서드(F#)

에이전트를 만들고 시작합니다.

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

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

// Signature:
static member Start : (MailboxProcessor<'Msg> -> Async<unit>) * ?CancellationToken -> MailboxProcessor<'Msg>

// Usage:
MailboxProcessor.Start (body)
MailboxProcessor.Start (body, cancellationToken = cancellationToken)

매개 변수

반환 값

만들어진 MailboxProcessor입니다.

설명

body 함수는 에이전트에 의해 실행되는 비동기 계산을 생성하는 데 사용됩니다.

예제

다음 코드 예제에서는 사서함 프로세서 에이전트를 시작하는 방법을 보여 줍니다. 이 예제에서 콘솔에서 입력되는 각 줄은 메시지 대기열에 게시됩니다. 프로그램이 각 메시지를 읽고 응답 채널을 사용하여 회신합니다. 특수 메시지 "Stop"을 받으면 회신 중지가 보내지고 프로그램이 종료됩니다.

open System

type Message = string * AsyncReplyChannel<string>

let formatString = "Message number {0} was received. Message contents: {1}" 

let printThreadId note =

    // Append the thread ID.
    printfn "%d : %s" System.Threading.Thread.CurrentThread.ManagedThreadId note


let agent = MailboxProcessor<Message>.Start(fun inbox ->
    let rec loop n =
        async {
                let! (message, replyChannel) = inbox.Receive();
                printThreadId "MailboxProcessor" 
                if (message = "Stop") then
                    replyChannel.Reply("Stopping.")
                else
                    replyChannel.Reply(String.Format(formatString, n, message))
                do! loop (n + 1)
        }
    loop 0)

printfn "Mailbox Processor Test"
printfn "Type some text and press Enter to submit a message."
printfn "Type 'Stop' to close the program." 

let rec loop() =
    printf "> " 
    let input = Console.ReadLine()
    printThreadId("Console loop")
    let reply = agent.PostAndReply(fun replyChannel -> input, replyChannel)
    if (reply <> "Stopping.") then
        printfn "Reply: %s" reply
        loop()
    else
        ()
loop()

printfn "Press Enter to continue."
Console.ReadLine() |> ignore

세션의 예를 들면 다음과 같습니다.

  
  
  
  
  
  
  
  
  

플랫폼

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

버전 정보

F# 코어 라이브러리 버전

지원: 2.0, 4.0, 노트북

참고 항목

참조

Control.MailboxProcessor<'Msg> 클래스(F#)

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