简体   繁体   English

使用httprequest后台工作程序将UI冻结1或2秒

[英]UI freezes for 1 or 2 sec using httprequest background worker

What's wrong with my code? 我的代码有什么问题? Is the background worker not properly setup causing the UI to freeze? 后台工作程序是否未正确设置,导致UI冻结? It appears that the delay starts when BeginGetResponse is called and then resume normally after the result is returned from the web server. 似乎延迟开始于调用BeginGetResponse时,然后从Web服务器返回结果后正常恢复。

    private void updateProgressbar()
    {
        bgWorker = new BackgroundWorker();
        bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
        bgWorker.RunWorkerAsync();
    }

    private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        string path = "http://www.somestring.com/script.php?a=b");
        Uri uriString = new Uri(path);

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uriString);
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)";
        request.BeginGetResponse(new AsyncCallback(ReadCallback), request);
    }

    private void ReadCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);

            using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
            {
                string resultString = streamReader1.ReadToEnd();
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        JsonMainProgressbar progressBarValue;
                        progressBarValue = JsonConvert.DeserializeObject<JsonMainProgressbar>(resultString);
                        this.ProgressBar.Value = Convert.ToInt32(progressBarValue.userclicks / progressBarValue.countryclicks * 100);
                        this.txtContribution.Text = "your contribution: " + this.ProgressBar.Value + "%";
                        Debug.WriteLine("Progressbar updated");
                    });
            }

     }

I think you're doing too much work to the UI thread and not enough on the background thread that ReadCallback is on. 我认为您对UI线程做了太多工作,而在后台线程上ReadCallback却做得不够。 Try moving as much as you can (*) outside the lambda function you pass to BeginInvoke() . 尝试在传递给BeginInvoke()的lambda函数之外尽可能多地(*)移动。

(*) ie safely without InvalidCrossThreadExceptions or race conditions... (*),即安全无InvalidCrossThreadExceptions或竞争条件...

In this case try something like: 在这种情况下,请尝试以下操作:

string resultString = streamReader1.ReadToEnd();
JsonMainProgressbar progressBarValue;
progressBarValue = JsonConvert.DeserializeObject<JsonMainProgressbar>(resultString);
int progressBarValueInt = Convert.ToInt32(progressBarValue.userclicks /
        progressBarValue.countryclicks * 100);

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
    this.ProgressBar.Value = progressBarValueInt;
    this.txtContribution.Text = "your contribution: " + progressBarValueInt + "%";
    Debug.WriteLine("Progressbar updated");
 });

(assuming you can use JsonMainProgressBar safely in the background thread) (假设您可以在后台线程中安全地使用JsonMainProgressBar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM