0

如何将参数传递给 Async.RunSynchronously?

我正在尝试执行以下操作:

Async.RunSynchronously (moveAsync brick)

当然,这不会编译:

未定义值或构造函数“brick”

我更新了我的代码,但仍然遇到关于将参数传递给 Async.RunSynchronously 的相同问题

客户:

打开乐高命令

[<EntryPoint>]
let main argv =
    connectAsync |> Async.RunSynchronously |> ignore
    moveAsync    |> Async.RunSynchronously |> ignore
    speakAsync   |> Async.RunSynchronously |> ignore

    0 // return an integer exit code

领域:

目前我的代码通过设置一个外部成员变量并让我的函数引用它来工作。

let brick = Brick(UsbCommunication())

我不想要这个。

module LegoCommands

open Lego.Ev3.Core
open Lego.Ev3.Desktop
open System.Threading.Tasks
open Arguments

let brick = Brick(UsbCommunication())

let awaitTask (task: Task) = task |> Async.AwaitIAsyncResult
                                  |> Async.Ignore
let connectAsync = async {
    do! brick.ConnectAsync() |> awaitTask }

let moveAsync = async {
    do! brick.DirectCommand.TurnMotorAtPowerForTimeAsync(motors, power, uint32 duration, breakEnabled) |> awaitTask }

let speakAsync = async {
    do! brick.DirectCommand.PlayToneAsync(volume, frequency, duration) |> awaitTask }
4

2 回答 2

0

在您正在使用的“客户端”的第三行brick上,此时尚未定义。

Async.RunSynchronously (connectAsync brick)

“域”的最后一行也发生了同样的情况:

Async.RunSynchronously (moveAsync(brick))

错误消息准确地告诉您:brick未定义。

于 2016-02-21T14:31:04.230 回答
0

我不确定我做错了什么。

但我不再观察到错误。

客户:

open LegoCommands
open Lego.Ev3.Core
open Lego.Ev3.Desktop

[<EntryPoint>]
let main argv =
    let brick = Brick(UsbCommunication())
    brick |> connectAsync |> Async.RunSynchronously |> ignore
    brick |> moveAsync    |> Async.RunSynchronously |> ignore
    brick |> speakAsync   |> Async.RunSynchronously |> ignore

    0 // return an integer exit code

领域:

module LegoCommands

open Lego.Ev3.Core
open Lego.Ev3.Desktop
open System.Threading.Tasks
open Arguments

let awaitTask (task: Task) = task |> Async.AwaitIAsyncResult
                                  |> Async.Ignore
let connectAsync (brick:Brick) = async {
    do! brick.ConnectAsync() |> awaitTask }

let moveAsync (brick:Brick) = async {
    do! brick.DirectCommand.TurnMotorAtPowerForTimeAsync(motors, power, uint32 duration, breakEnabled) |> awaitTask }

let speakAsync (brick:Brick) = async {
    do! brick.DirectCommand.PlayToneAsync(volume, frequency, duration) |> awaitTask }

参数:

module Arguments
open Lego.Ev3.Core

let volume = 50
let frequency = uint16 3000
let duration = uint16 333
let power = 100
let motors = OutputPort.B ||| OutputPort.C
let breakEnabled = false
于 2016-02-21T21:50:30.360 回答