0

I have auto-upload application from ftp server and two progressbar's to update overall and current download state. First one works fine (update% = currentFile as int / allFilesToDownload *100%). But i'd like to upload current file downloading. My code:

Uri url = new Uri(sUrlToDnldFile);

                    int inde = files.ToList().IndexOf(file);
                    string subPath = ...
                    bool IsExists = System.IO.Directory.Exists(subPath);
                    if (!IsExists)
                        System.IO.Directory.CreateDirectory(subPath);
                    sFileSavePath = ...
                    System.Net.FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(file));

                    System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)request.GetResponse();
                    response.Close();
                    long iSize = response.ContentLength;
                    long iRunningByteTotal = 0;

                    WebClient client = new WebClient();

                    Stream strRemote = client.OpenRead(url);

                    FileStream strLocal = new FileStream(sFileSavePath, FileMode.Create, FileAccess.Write, FileShare.None);

                    int iByteSize = 0;

                    byte[] byteBuffer = new byte[1024];

                    while ((iByteSize = strRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                    {
                        strLocal.Write(byteBuffer, 0, iByteSize);
                        iRunningByteTotal += iByteSize;
//THERE I'D LIKE TO UPLOAD CURRENT FILE DOWNLOAD STATUS
                        string a = iByteSize.ToString();
                        double b = double.Parse(a.ToString()) / 100;
                        string[] c = b.ToString().Split(',');
                        int d = int.Parse(c[0].ToString());
                        update(d);
//update(int prog) {  bgWorker2.ReportProgress(prog); }


                    }

                    double dIndex = (double)(iRunningByteTotal);
                    double dTotal = (double)iSize;
// THIS CODE COUNTING OVERAL PROGRESS - WORKS FINE
                    double iProgressPercentage1 = double.Parse(ind.ToString()) / double.Parse(files.Count().ToString()) * 100;
                    ind++;
                    string[] tab = iProgressPercentage1.ToString().Split(',');

                    int iProgressPercentage = int.Parse(tab[0]);
                    currentFile = file;
                    bgWorker1.ReportProgress(iProgressPercentage);
                    strRemote.Close();

Unfortunately i still getting error, that I cant update progressBar2, becouse another process using it. Is there any way to do it? Thanks

user13657
  • 745
  • 3
  • 17
  • 36
  • 1
    Process? You mean [thread](http://stackoverflow.com/questions/11923865/how-to-deal-with-cross-thread-access-exceptions), right? – H.B. Aug 07 '13 at 11:17

1 Answers1

1

update values thru dispatcher.BeginInvoke Methods something like.

Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(()=>
{
  progressbar2.value = newvalue;
}));

This Dispatcher Will push you work to the main thread which is holding the Progressbar2.

JSJ
  • 5,653
  • 3
  • 25
  • 32