Nota
O acesso a esta página requer autorização. Pode tentar iniciar sessão ou alterar os diretórios.
O acesso a esta página requer autorização. Pode tentar alterar os diretórios.
Digitalizações para uma mensagem procurando pelas mensagens na ordem de chegada até que uma função fornecida retornar um valor de Some .Outras mensagens permanecem na fila.
Namespace/Module Path: Microsoft.FSharp.Control
Assembly: FSharp.Core (em FSharp.Core.dll)
// Signature:
member this.TryScan : ('Msg -> Async<'T> option) * ?int -> Async<'T option>
// Usage:
mailboxProcessor.TryScan (scanner)
mailboxProcessor.TryScan (scanner, timeout = timeout)
Parâmetros
scanner
Tipo: 'Msg ->Async<'T>opçãoUma função que retorna None se a mensagem deve ser tiver ignorado Some , ou se a mensagem deve ser processada e removido da fila.
timeout
Tipo: intUm tempo limite em milissegundos opcional.Padrão é -1 que corresponde a Infinite.
Valor de retorno
Uma computação assíncrono (objeto deAsync ) que scanner compilado fora da mensagem de leitura.
Comentários
Se um tempo limite é excedido, None será retornado.Este método é para uso dentro do corpo de agente.Para cada agente, no máximo um leitor simultânea pode ser ativo, o que não mais do que uma chamada a Recebersimultânea, a TryReceive, a Verificação ou a TryScan podem ser ativos.O corpo da função de scanner é bloqueado durante sua execução, mas o bloqueio será liberado antes da execução de fluxo de trabalho assíncrono.
Exemplo
O exemplo de código a seguir mostra como usar o método de TryScan .Este exemplo é um agente de submissão de trabalhos.Há três agentes: se chamado runAgent que comece cada trabalho, outro inprogressAgent chamado que representa todos os trabalhos em execução, e se chama completeAgent que representa a notificação informando que o trabalho estejam concluídos.TryScan é usado na função de cancelJob para localizar um trabalho, ou exclusão falhar se não houver nenhum trabalho correspondente.
open System
open System.Threading
let random = System.Random()
// Generates mock jobs by using Async.Sleep.
let createJob(id:int, source:CancellationTokenSource) =
let job = async {
// Let the time be a random number between 1 and 10000.
// The mock computed result is a floating point value.
let time = random.Next(10000)
let result = random.NextDouble()
let count = ref 1
while (!count <= 100 && not source.IsCancellationRequested) do
do! Async.Sleep(time / 100)
count := !count + 1
return result
}
id, job, source
type Result = double
// A Job consists of a job ID, a computation that produces a single result,
// and a cancellation token source object that can be used to cancel the job.
type Job = int * Async<Result> * CancellationTokenSource
type Message = int * Result
let context = System.Threading.SynchronizationContext.Current
// This agent processes when jobs are completed.
let completeAgent = MailboxProcessor<Message>.Start(fun inbox ->
let rec loop n =
async {
let! (id, result) = inbox.Receive()
printfn "The result of job #%d is %f" id result
do! loop (n + 1)
}
loop (0))
// inprogressAgent maintains a queue of in-progress jobs that can be
// scanned to remove canceled jobs. It never runs its processor function,
// so we set it to do nothing.
let inprogressAgent = new MailboxProcessor<Job>(fun _ -> async { () })
// This agent starts each job in the order in which it is received.
let runAgent = MailboxProcessor<Job>.Start(fun inbox ->
let rec loop n =
async {
let! (id, job, source) = inbox.Receive()
printfn "Starting job #%d" id
// Post to the in-progress queue.
inprogressAgent.Post(id, job, source)
// Start the job.
Async.StartWithContinuations(job,
(fun result -> completeAgent.Post(id, result)),
(fun _ -> ()),
(fun cancelException -> printfn "Canceled job #%d" id),
source.Token)
do! loop (n + 1)
}
loop (0))
for id in 1 .. 10 do
let source = new CancellationTokenSource()
runAgent.Post(createJob(id, source))
let cancelJob(cancelId) =
Async.RunSynchronously(
inprogressAgent.TryScan((fun (jobId, result, source) ->
let action =
async {
printfn "Canceling job #%d" cancelId
source.Cancel()
return cancelId
}
// Return Some(async) if the job ID matches.
if (jobId = cancelId) then
Some(action)
else
None), 1000))
printfn "Specify a job by number to cancel it, then press Enter."
let mutable finished = false
while not finished do
let input = System.Console.ReadLine()
let a = ref 0
if (Int32.TryParse(input, a) = true) then
match cancelJob(!a) with
| Some id -> printfn "A job was canceled: job #%d" id
| None -> printfn "Job not found."
else
printfn "Terminating."
finished <- true
A seguir está uma sessão exemplo.
Plataformas
O windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2
Informações de Versão
Versões da biblioteca principal de F#
Suportado em: 2,0, 4,0, portáteis