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

时间:2016-02-21 13:50:09

标签: f# mindstorms

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

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

Async.RunSynchronously (moveAsync brick)

当然,这不会编译:

  

价值或构造函数' brick'未定义

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

客户端:

打开LegoCommands

[<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 }

2 个答案:

答案 0 :(得分:0)

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

Async.RunSynchronously (connectAsync brick)

“域名”的最后一行也是如此:

Async.RunSynchronously (moveAsync(brick))

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

答案 1 :(得分: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