Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

问题:ILRuntime层调用async方法时,如果方块内没有await代码块,则不会有返回,请问下这个问题如何处理? #23

Open
Weishaoorz opened this issue Dec 22, 2023 · 1 comment

Comments

@Weishaoorz
Copy link

问题:ILRuntime层调用async方法时,如果方块内没有await代码块,则不会有返回,请问下这个问题如何处理?
复现方式:
1、我们使用的是这个链接下的实例,https://github.com/Ourpalm/ILRuntimeU3D/tree/master/ILRuntimeDemo。
2、在这个实例中,我们加入了一个处理async/await的跨域适配器,该适配器代码已打包成zip。
AsyncStateMachineAdaptor.zip
3、我们在Assets/Samples/ILRuntime/2.1.0/_Scenes/Examples/01_HelloWorld的场景里的nitializeILRuntime()方法中加入跨域注册的代码:appdomain.RegisterCrossBindingAdaptor(new AsyncStateMachineAdaptor());
image
4、我们在ILRuntime的工程中的StaticFunTest()中加入逻辑代码:
public static async void StaticFunTest()
{
UnityEngine.Debug.Log("!!! InstanceClass.StaticFunTest()");
string s = await SetAsyncTest(false);
UnityEngine.Debug.Log(s);
UnityEngine.Debug.Log("!!! 111111111");
}

    public static async Task<string> SetAsyncTest(bool flag)
    { 
        if (flag == true) 
        {
            await Task.Delay(1000);
            UnityEngine.Debug.Log("测试有await");
            return "测试有awaitReturn";
        }
        else
        {
            //如果是执行这段代码,如是没有下面的await Task.Yield()代码块,则不能接收到return的返回,
            //当加入await Task.Yield()代码块后,才能正常的返回。
            //尝试使用Task.FromResult("");也不行。
            //await Task.Yield();
            UnityEngine.Debug.Log("测试没有await");
            return "测试没有awaitReturn";
        }

    }

image
5、我们获取到测试结果:
当以上方法中flag=true的时候,方法中有await Task.Delay代码块,能正常获取到返回值。
当以上方法中flag=false的时候,方法中没有await Task代码块,无法正常获取到返回,也就是说
StaticFunTest方法中的如下代码不会执行。
UnityEngine.Debug.Log(s);
UnityEngine.Debug.Log("!!! 111111111");

@Weishaoorz
Copy link
Author

做了反复测试,热更层将Debug换成Release就正常了。
所以切换一下,感谢ILRuntime群巨佬群主一起排查这个问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant