我正在构建一个包含F#文件的项目,在构建期间第39行出现错误:
预计此表达式的类型为Async<' a>但这里有类型DispatcherOperation
open System
open System.Collections.Generic
open System.Collections.ObjectModel
open System.Linq
open System.Net
open System.Reactive.Disposables
open System.Runtime.CompilerServices
open System.Threading
open System.Windows
open System.Windows.Threading
open Microsoft.Practices.Unity
type IActivityContext<'TResult> = interface
abstract container: IUnityContainer
abstract Success: result:'TResult -> unit
abstract Error: error:Exception -> unit
abstract Cancel: unit -> unit
abstract RegisterCancellationCallback: callback:Action->IDisposable
end
[<Extension>]
type ActivityExtensions private() = class
[<Extension>]
static member StartViewActivity<'TResult>(container: IUnityContainer, callback: Action<IActivityContext<'TResult>>)= async{
let! ct = Async.CancellationToken
return! Async.FromContinuations(fun (success, error, cancel) ->
let context = {
new IActivityContext<'TResult> with
member this.container = container
member this.Success(result:'TResult) = success(result)
member this.Error(err:Exception) = error(err)
member this.Cancel() = cancel(new OperationCanceledException())
member this.RegisterCancellationCallback(callback: Action) =
ct.Register(callback) :> IDisposable
}
let disp = Application.Current.Dispatcher
Async.StartWithContinuations(
(* ERROR -> *) disp.InvokeAsync((fun() -> callback.Invoke(context))),
(fun()->()),
error,
cancel,
ct
)
)
}
end
有谁知道为什么会出现这个错误,解决方案是什么?
答案 0 :(得分:4)
错误消息显示全部:Dispatcher.InvokeAsync
返回DispatcherOperation
,而Async.StartWithContinuations
则需要Async
。
您可以等待DispatcherOperation.Task
:
Async.StartWithContinuations(
disp.InvokeAsync((fun() -> callback.Invoke(context))).Task |> Async.AwaitTask,
ignore,
error,
cancel,
ct)
请注意,使用async
包装表达式不会自动使其成为异步,并且在不等待两次的情况下包装已经异步的流甚至可能根本不执行内部异步:
let print = async { printfn "Can we observe this?" }
async { return print } |> Async.RunSynchronously // nothing printed
async { return! print } |> Async.RunSynchronously // prints message
所以这不是异步的:
Async.StartWithContinuations(
async { disp.Invoke((fun() -> callback.Invoke(context))) },
ignore,
error,
cancel,
ct)
这甚至可能不会调用回调(取决于InvokeAsync
实现,但Task
通常是隐式启动的):
Async.StartWithContinuations(
async { disp.InvokeAsync((fun() -> callback.Invoke(context))) },
ignore,
error,
cancel,
ct)
最重要的是,在内部回调返回之前,回调(通常是成功的,在此处:ignore
)可能被称为。
答案 1 :(得分:1)
我已经解决了我的问题。
方法“StartWithContinuations”具有类型为Async&lt;'T&gt;的第一个参数“calculate”。所以我用async {}包含了第一个参数。
let disp = Application.Current.Dispatcher
Async.StartWithContinuations(
async {disp.InvokeAsync((fun() -> callback.Invoke(context)))},
ignore,
error,
cancel,
ct
)
)