Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VB.NET
Hello Code Project,
I am making one auto updater for game in Windows Form Application(C#), and i want to show in program which file is on downloading progress,
I did Success to show how many % download done and also i did success to show downloading on Progress Bar.
But i am getting failed to show on Label which File is Currently Downloading.
My code is here
 
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
   //Defines the server's update directory
   string Server = "Server URL";
 
   //Defines application root
   string Root = AppDomain.CurrentDomain.BaseDirectory;
 
   //checks client version
   string lclVersion;
   using (StreamReader reader = new StreamReader("version"))
   {
      lclVersion = reader.ReadLine();
   }
   decimal localVersion = decimal.Parse(lclVersion);
 
   //server's list of updates
   XDocument serverXml = XDocument.Load(@Server + "version.xml");
 
   //The Update Process
   foreach (XElement update in serverXml.Descendants("update"))
   {
      string version = update.Element("version").Value;
      string file = update.Element("file").Value;
 
      decimal serverVersion = decimal.Parse(version);
 
      string sUrlToReadFileFrom = Server + file;
 
      string sFilePathToWriteFileTo = Root + file;
 
      if (serverVersion > localVersion)
      {
         Uri url = new Uri(sUrlToReadFileFrom);
         System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
         System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
         response.Close();
 
         Int64 iSize = response.ContentLength;
         Int64 iRunningByteTotal = 0;
 
         using (System.Net.WebClient client = new System.Net.WebClient())
         {
            using (System.IO.Stream streamRemote = client.OpenRead(new Uri(sUrlToReadFileFrom)))
            {
               using (Stream streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None))
               {
                  int iByteSize = 0;
                  byte[] byteBuffer = new byte[iSize];
                  while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                  {
                     streamLocal.Write(byteBuffer, 0, iByteSize);
                     iRunningByteTotal += iByteSize;
 
                     double dIndex = (double)(iRunningByteTotal);
                     double dTotal = (double)byteBuffer.Length;
                     double dProgressPercentage = (dIndex / dTotal);
                     int iProgressPercentage = (int)(dProgressPercentage * 100);
                     backgroundWorker1.ReportProgress(iProgressPercentage);    
                  }
 
                  streamLocal.Close();
               }
 
               streamRemote.Close();
            }
         }
      }
   }
}
 
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
   label3.ForeColor = System.Drawing.Color.White;
   label3.Text = e.ProgressPercentage.ToString() + "%";
   progressBar1.Value = e.ProgressPercentage;
   downloadLbl.ForeColor = System.Drawing.Color.White;
   downloadLbl.Text = "Downloading Updates";
}

 
Thanks in Advance Smile | :)
Posted 9-Aug-14 2:38am
Edited 13-Aug-14 4:18am
Nelek51.4K
v3
Comments
digimanus at 11-Aug-14 5:28am
   
what if you create a String object in the class, fill it in DoWork and show it in ProgressChanged?
UttamRabadiya at 11-Aug-14 8:14am
   
hmm thank you sir i will try it soon :))
UttamRabadiya at 12-Aug-14 6:13am
   
i try but its failed :((
digimanus at 13-Aug-14 8:33am
   
what was the exception? something about threading?
digimanus at 13-Aug-14 10:20am
   
if you want to show in a textbox, you have to Invoke! the textBox.
Read about that
UttamRabadiya at 13-Aug-14 8:37am
   
i have already string file that have the downloading file name and when i try to put the string file on ProgressChanged..it will say the name 'file' does not exists in current context
UttamRabadiya at 13-Aug-14 8:39am
   
i was thinking if i even can print the value from XML file like if version = 1.2 than it will read file name for version 1.2. i just have this idea but i am still thinking how i am gonna apply it :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Finally Solve it Smile | :) )
i just adjusted some codes like here
in while Progress in DoWork.
downloadUpdates.ReportProgress(iProgressPercentage,file);
 
and another change on ProgressChanged.
 
 var filename = Convert.ToString(e.UserState);
 label1.Text = "Downloading Update File " + filename;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I use WebClient.DownloadProgressChanged[^] and DownloadDataAsync [^] to that sort for work.
 
DownloadDataAsync starts downloading and DownloadProgressChanged events tells how much data i downloaded and DownloadDataComplete event informs when download is completed. And the bonus with DownloadDataAsync, you can cancel the download.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 841
1 OriginalGriff 245
2 Volynsky Alex 205
3 Kishore Pogaru 140
4 Magic Wonder 129
0 OriginalGriff 5,640
1 Sergey Alexandrovich Kryukov 4,693
2 CPallini 4,540
3 George Jonsson 3,132
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 17 Aug 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100