【问题标题】:Unity Engine freezes when Accessing Task<Token>.Result访问 Task<Token>.Result 时 Unity 引擎冻结
【发布时间】:2022-01-14 06:08:44
【问题描述】:

我已经实现了一些代码来访问受 oauth 2.0 保护的 api。一旦我点击“播放”按钮,Unity 引擎就会冻结。我必须通过任务管理器关闭它。

我的代码:

public class importFromAPI : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        HttpClient client = new HttpClient();
        Task<Token> task = GetElibilityToken(client);
        //Token token = task.Result;
    }

    // Update is called once per frame
    void Update()
    {

    }


    // OAuth Stuff




private static async Task<Token> GetElibilityToken(HttpClient client)
{
    string baseAddress = @"*REDACTED*/oauth/token";

    string grant_type = "client_credentials";
    string client_id = "*REDACTED*";
    string client_secret = "*REDACTED*";
    string audience = "*REDACTED*";

    var form = new Dictionary<string, string>
        {
            {"grant_type", grant_type},
            {"client_id", client_id},
            {"client_secret", client_secret},
            {"audience", audience }
        };

    HttpResponseMessage tokenResponse = await client.PostAsync(baseAddress, new FormUrlEncodedContent(form));
        var jsonContent = await tokenResponse.Content.ReadAsStringAsync();
        Debug.Log("Response: " + jsonContent);
        Token tok = JsonConvert.DeserializeObject<Token>(jsonContent);
    Debug.Log(tok.ToString());
    return tok;
}


internal class Token
{
    [JsonProperty("access_token")]
    public string AccessToken { get; set; }

    [JsonProperty("token_type")]
    public string TokenType { get; set; }

    [JsonProperty("expires_in")]
    public int ExpiresIn { get; set; }

    [JsonProperty("refresh_token")]
    public string RefreshToken { get; set; }

    override
    public string ToString()
    {
        return "AccessToken: " + AccessToken + "; TokenType: " + TokenType + "; Expires in: " + ExpiresIn + "; Refresh Token: " + RefreshToken;
    }
}
}

从 Start() 方法中删除 task.Result 会阻止 Unity 在按下按钮时冻结,但我想获取令牌以访问 api。

谁能告诉我为什么 Unity 会死机?

【问题讨论】:

  • Can anyone tell me why Unity freezes? - 因为它是deadlocks。
  • 这不是死锁,线程只是被阻塞直到任务完成。

标签: c# unity3d async-await


【解决方案1】:

您需要使用await 关键字,如下所示:

Token task = await GetElibilityToken(client);

但是你也需要改变方法,像这样:

async Task Start()

实际上,建议始终使用异步。您可以在本主题here 中找到更多信息。

https://gametorrahod.com/unity-and-async-await/

编辑:根据 cmets,似乎我们不允许将 Unity 的 Start 更改或重新定义为 async Task,所以在这种情况下我仍会使用 async void ,尽管不推荐https://docs.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming

async void Start()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 2018-03-30
    • 2019-02-18
    • 2016-03-02
    相关资源
    最近更新 更多