簡體   English   中英

如何編寫異步更新方法以使用 RestSharp 傳遞兩個參數?

[英]How do I write an async Update method to pass two parameters using RestSharp?

我正在 Unity 上開發機器人應用程序,我想將兩個參數從我的應用程序連續傳遞給客戶端。 我編寫的代碼導致應用程序運行非常緩慢(大約每 5 秒 1 幀),這一定是我的異步請求的問題,但我不確定問題出在哪里:

 async void FixedUpdate ()
{
 
        var client = new RestClient("http://localhost/rw/motionsystem/mechunits/ROB_1?action=mechunit-position");

        client.Timeout = -1;

        client.CookieContainer = login_cookie;

        var request = new RestRequest(Method.POST);

        request.AddHeader("Accept", "application/json");
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded;v=2.0");

        request.AddParameter("rob_joint", manualjog);
        request.AddParameter("ext_joint", "[0,0,0,0,0,0]");

        var restResponse = await client.ExecuteTaskAsync(request);
   

}

我剛剛用一個專用的 function 和一個 while 循環解決了它。 正如所指出的,問題出在 FixedUpdate 中。 這是工作代碼:

public async void SyncMechanicalUnits() {

    var client = new RestClient("http://localhost/rw/motionsystem/mechunits/ROB_1?action=mechunit-position");
    client.Timeout = -1;
    client.CookieContainer = login_cookie;

    while (solveron == true)
    {
        var request = new RestRequest(Method.POST);

        request.AddHeader("Accept", "application/json");
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded;v=2.0");

        request.AddParameter("rob_joint", manualjog);
        request.AddParameter("ext_joint", "[0,0,0,0,0,0]");

        var restResponse = await client.ExecuteTaskAsync(request);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM