如何将参数传递给 Async.RunSynchronously?
How do I pass an argument into Async.RunSynchronously?
如何将参数传递给 Async.RunSynchronously?
我正在尝试执行以下操作:
Async.RunSynchronously (moveAsync brick)
当然这个编译不了:
The value or constructor 'brick' is not defined
我更新了我的代码,但仍然 运行 遇到关于将参数传递给 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 }
在 "client" 的第三行,您使用的是 brick
,此时尚未定义。
Async.RunSynchronously (connectAsync brick)
同样发生在 "domain" 的最后一行:
Async.RunSynchronously (moveAsync(brick))
错误消息准确地告诉您:brick
未定义。
我不确定我做错了什么。
但我不再观察到错误。
客户:
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
如何将参数传递给 Async.RunSynchronously?
我正在尝试执行以下操作:
Async.RunSynchronously (moveAsync brick)
当然这个编译不了:
The value or constructor 'brick' is not defined
我更新了我的代码,但仍然 运行 遇到关于将参数传递给 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 }
在 "client" 的第三行,您使用的是 brick
,此时尚未定义。
Async.RunSynchronously (connectAsync brick)
同样发生在 "domain" 的最后一行:
Async.RunSynchronously (moveAsync(brick))
错误消息准确地告诉您:brick
未定义。
我不确定我做错了什么。
但我不再观察到错误。
客户:
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