简体   繁体   中英

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? It appears that the delay starts when BeginGetResponse is called and then resume normally after the result is returned from the web server.

    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. Try moving as much as you can (*) outside the lambda function you pass to BeginInvoke() .

(*) ie safely without InvalidCrossThreadExceptions or race conditions...

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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