RestSharp异步API - 更新GUI

时间:2013-05-07 21:47:15

标签: c# restsharp

我在打开表单时使用C#中的RestSharp调用Restful API,但是在调用它时GUI会打开并锁定。

只是想知道我做错了什么......

我在Invoke和BeginInvoke之间进行了更改,但没有运气 - Debug输出似乎挂在第一个和第一个之间。第二次调用调用

public void show_members(String url, String api_key, String card_number)
{
    member_numbers.Clear();
    st_url = url;
    st_api_key = api_key;
    st_card_number = card_number;

    var client = new RestClient(url);

    var request = new RestRequest("pendingmembers?api_key={api_key}", Method.GET);
    request.AddUrlSegment("api_key", api_key);

    Debug.WriteLine(url + "pendingmembers?api_key=" + api_key);

    // async with deserialization
    var asyncHandle = client.ExecuteAsync<PendingMembers>(request, response =>
    {
        Debug.WriteLine(response.Data.status);
        if (response.Data.status == "OK")
        {
            //Company ID is Correct
            //Dispatcher.Invoke((Action)() => {cb_MemberList.Items.Clear();});
            if (this.InvokeRequired)
            {
                this.Invoke((Action)(() =>
                {
                    cb_MemberList.Items.Clear();
                }));
            }
            else
            {
                cb_MemberList.Items.Clear();
            }
            //Debug.WriteLine(response.Data.members);
            foreach(var member in response.Data.members)
            {
                Debug.WriteLine(member.full_name);
                member_numbers.Add(member.member_id);
                if (this.InvokeRequired)
                {
                    this.Invoke((Action)(() =>
                    {
                        Debug.WriteLine(member.full_name);
                        cb_MemberList.Items.Add(member.full_name);
                    }));
                }
                else
                {
                    Debug.WriteLine(member.full_name);
                    cb_MemberList.Items.Add(member.full_name);
                }
            }
            Application.DoEvents();
        }
    });
}

1 个答案:

答案 0 :(得分:0)

解决了这个问题 - 在评论整个Web API调用仍然导致UI挂起时,效果非常好。

在检查时,window.Show()而不是导致问题的window.ShowDialog() ......

故事的道德,更多的睡眠!