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

업데이트: 2011년 1월

메시지를 기다리고 도착 순서대로 첫 번째 메시지를 사용합니다.

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

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

// Signature:
member this.Receive : ?int -> Async<'Msg>

// Usage:
mailboxProcessor.Receive ()
mailboxProcessor.Receive (timeout = timeout)

매개 변수

  • timeout
    형식: int

    선택적 제한 시간(밀리초)입니다. 기본값은 Infinite()에 해당하는 -1입니다.

예외

예외

조건

TimeoutException

시간 제한이 초과된 경우 throw됩니다.

반환 값

받은 메시지를 반환하는 비동기 계산(Async 개체)입니다.

설명

이 메서드는 에이전트 본문 내에서 사용됩니다. 각 에이전트에 대해 최대 하나의 동시 판독기만 활성화될 수 있으므로 Receive, TryReceive, Scan 또는 TryScan에 대한 둘 이상의 동시 호출이 활성화될 수 없습니다.

예제

다음 예제에서는 Receive 메서드를 사용하는 방법을 보여 줍니다. 이 경우 10초의 시간 제한이 지정됩니다. 일반적으로 메시지 처리 함수는 게시물 기능과 다른 스레드에서 실행되므로 메시지 프로세서 함수에서 시간 제한 예외를 catch해야 합니다. 이 예제에서는 시간 제한 예외로 인해 루프가 계속되고 메시지 번호는 1씩 증가합니다.

open System

type Message = string * AsyncReplyChannel<string>

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

let agent = MailboxProcessor<Message>.Start(fun inbox ->
    let rec loop n =
        async {            
            try
                let! (message, replyChannel) = inbox.Receive(10000);

                if (message = "Stop") then
                    replyChannel.Reply("Stop")
                else
                    replyChannel.Reply(String.Format(formatString, n, message))
                do! loop (n + 1)

            with
            | :? TimeoutException -> 
                printfn "The mailbox processor timed out."
                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()
    let reply = agent.PostAndReply(fun replyChannel -> input, replyChannel)
    if (reply <> "Stop") then
        printfn "Reply: %s" reply
        loop()
    else
        ()
loop()

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

일반 세션은 다음과 같습니다. 시간 제한으로 인해 메시지 2를 건너 뛰었다는 점에 주목하십시오.

                    

플랫폼

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.MailboxProcessor<'Msg> 클래스(F#)

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

변경 기록

날짜

변경 내용

이유

2011년 1월

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

향상된 기능 관련 정보