ProgressBar isn't updating

Posted by Nuru Salihu on Stack Overflow See other posts from Stack Overflow or by Nuru Salihu
Published on 2013-11-06T03:31:42Z Indexed on 2013/11/06 3:54 UTC
Read the original article Hit count: 176

Filed under:
|
|

I have a progressbar that that is show progress returned by the backgroundworker do_dowork event like below .

       if (ftpSourceFilePath.Scheme == Uri.UriSchemeFtp)
            {
                FtpWebRequest objRequest = (FtpWebRequest)FtpWebRequest.Create(ftpSourceFilePath);
                NetworkCredential objCredential = new NetworkCredential(userName, password);
                objRequest.Credentials = objCredential;
                objRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                FtpWebResponse objResponse = (FtpWebResponse)objRequest.GetResponse();
                StreamReader objReader = new StreamReader(objResponse.GetResponseStream());
                int len = 0;
                int iProgressPercentage = 0;
                FileStream objFS = new FileStream((cd+"\\VolareUpdate.rar"), FileMode.Create, FileAccess.Write, FileShare.Read);

                    while ((len = objReader.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
                        {

                            objFS.Write(buffer, 0, len);
                            iRunningByteTotal += len;
                            double dIndex = (double)(iRunningByteTotal);
                            double dTotal = (double)buffer.Length;
                            double dProgressPercentage =  (dIndex / dTotal);

                            iProgressPercentage = (int)(dProgressPercentage);

                            if (iProgressPercentage > 100)
                            {
                                iProgressPercentage = 100;

                            }

                            bw.ReportProgress(iProgressPercentage);


                        }
}

However, my progressbar does not update. While searching , i was told the UI thread is being blocked and then i thought may be passing the progress outside the loop will do the trick. then i change to this

if (ftpSourceFilePath.Scheme == Uri.UriSchemeFtp)
                {
                    FtpWebRequest objRequest = (FtpWebRequest)FtpWebRequest.Create(ftpSourceFilePath);
                    NetworkCredential objCredential = new NetworkCredential(userName, password);
                    objRequest.Credentials = objCredential;
                    objRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                    FtpWebResponse objResponse = (FtpWebResponse)objRequest.GetResponse();
                    StreamReader objReader = new StreamReader(objResponse.GetResponseStream());
                    int len = 0;
                    int iProgressPercentage = 0;
                    FileStream objFS = new FileStream((cd+"\\VolareUpdate.rar"), FileMode.Create, FileAccess.Write, FileShare.Read);
    while ((len = objReader.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
                        {

                            objFS.Write(buffer, 0, len);
                            iRunningByteTotal += len;
                            double dIndex = (double)(iRunningByteTotal);
                            double dTotal = (double)buffer.Length;
                            double dProgressPercentage =  (dIndex / dTotal);

                            iProgressPercentage = (int)(dProgressPercentage);

                            if (iProgressPercentage > 100)
                            {
                                iProgressPercentage = 100;

                            }
                           // System.Threading.Thread.Sleep(2000);
                              iProgressPercentage++;
                         //    SetText("F", true);




                        }

                         bw.ReportProgress(iProgressPercentage);
                         progressBar.Refresh();

}

However still didn't help. When i put break point in my workerprogresschanged event, it show the progressbar.value however does not update. I tried progressbar.update(0, i also tried sleeping the thread for a while in the loop still didn't help. Please any suggestion/help would be appreciated .

© Stack Overflow or respective owner

Related posts about c#

Related posts about progress-bar