1

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

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

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

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();
        }
    });
}
4

1 回答 1

0

修复了这个问题——在注释掉整个 Web API 调用时工作得很好,仍然会导致 UI 挂起。

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

讲道理的故事,多睡觉!

于 2013-05-07T22:03:24.687 回答