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

为何不能直接使用await Task.Delay呢 #398

Open
AskrYggdrasillsCR opened this issue Jul 6, 2023 · 1 comment
Open

为何不能直接使用await Task.Delay呢 #398

AskrYggdrasillsCR opened this issue Jul 6, 2023 · 1 comment

Comments

@AskrYggdrasillsCR
Copy link

public class TestPatch : MonoBehaviour
{
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            CallOneAsync("hello");
        }
    }

    [Patch]
    public async void CallOneAsync(string name)
    {
        Debug.Log(name);
        await Task.Delay(1000);
        Debug.Log("TestTask");
    }
}

这样子写是不会等待一秒之后执行Debug.Log("TestTask");
但是像下面这样操作的话就可以正常执行

public class TestPatch : MonoBehaviour
{
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            CallOneAsync("hello");
        }
    }

    [Patch]
    public async void CallOneAsync(string name)
    {
        Debug.Log(name);
        await TestTask();
    }

    private async Task TestTask()
    {
        await Task.Delay(1000);
        Debug.Log("TestTask");
    }
}
@lishengru
Copy link

我遇到了同样得问题,一旦遇到await,代码执行顺序就会错乱

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

2 participants