如何拨打电话等待来自Lua/MoonSharp脚本的异步C#方法? [英] How to call & await async C# method from Lua / MoonSharp script?

查看:55
本文介绍了如何拨打电话等待来自Lua/MoonSharp脚本的异步C#方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打电话给&等待来自Lua/MoonSharp代码的C#异步方法.

I would like to call & await a C# async method from Lua / MoonSharp code.

例如:

1).

async void Test1() {
    await Something();
}

2).

async Task Test2() {
    await Something();
}

然后从Lua调用它-1).不会等待,而是继续执行脚本,以及2).抛出 ScriptRuntimeException:无法转换clr类型System.Threading.Tasks.Task`1 [System.Threading.Tasks.VoidTaskResult]MoonSharp.Interpreter.Interop.Converters.ClrToScriptConversions.ObjectToDynValue 例外.

And then calling it from Lua - 1). does not await but continues script execution, and 2). throws ScriptRuntimeException: cannot convert clr type System.Threading.Tasks.Task`1[System.Threading.Tasks.VoidTaskResult] MoonSharp.Interpreter.Interop.Converters.ClrToScriptConversions.ObjectToDynValue exception.

有什么办法可以使这项工作成功吗?

Is there some way to make this work?

推荐答案

我终于有了回调.不过,我认为这不是一个好的解决方案.因此,如果有人有更好的答案,我会很乐意更改接受的答案.

I've finally gone with callbacks. I don't think it's a good solution, though. So if anyone have a better one i'll be more than happy to change the accepted answer.

对于任何感兴趣的人,这里是如何使回调在MoonSharp中工作:

For anyone interested here's how to make the callbacks work in MoonSharp:

Lua/MoonSharp

Lua / MoonSharp

SomethingAsync(10, function()
    SomePrintFunction('async work done')
end)

C#

async void SomethingAsync(int whatever, DynValue callback) {
    await SomeAsyncWorkBeingDone();

    if (callback.Type == DataType.Function) {
        callback.Function.Call();
    }
}

更多信息可以在文档中找到.

More info can be found in the doc's.

这篇关于如何拨打电话等待来自Lua/MoonSharp脚本的异步C#方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆