locked
progressbar, backgrounworker (use a class1.cs) RRS feed

  • Question

  • Hi,
    My program:
    I download (class1.cs) files from an FTP server.
    This I want to see a progressbar.
    Because I use a class1.cs is I use a background worker

    Here is my project. c#
    https://rapidshare.com/files/2905061196/WindowsFormsApplication1_Downloader.zip
    If someone can put that in my project.

    Thank you!
    Jacky
    • Moved by CoolDadTx Monday, April 9, 2012 3:25 PM Winforms related (From:Visual C# General)
    Monday, April 9, 2012 9:09 AM

Answers

All replies

  • The problem is to show a progressbar when he is downloading the files

    i don't know how

    here some code:
    Form:

    namespace WindowsFormsApplication1
    {
        
        public partial class Form1 : Form
        {
            public  string pathName;
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 formke2 = new Form2();
                formke2.Show();
               
                Class1.pathName=textBox1.Text;
                Class1.CombineBlocksIntoLibrary(Class1.getpathName());
    
    
            }
        }
    }

    Class1.cs

    namespace WindowsFormsApplication1
    {
        class Class1
        {
            public static String pathName;
            public static String getpathName()
            {
                return pathName;
            }
    
            public static void CombineBlocksIntoLibrary(string pathName)
            {
                string activeDir = @"c:\Temp";
    
                //Create a new subfolder under the current active folder
                string newPath = System.IO.Path.Combine(activeDir, "test");
                
                // Create the subfolder
                System.IO.Directory.CreateDirectory(newPath);
    
                //File Download,Write Local Copy, from FTP Location
                string localPath = @"C:\Temp\test\";
    
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(pathName);
                request.Credentials = new NetworkCredential("LOGIN", "PASSWORD");
                request.Method = WebRequestMethods.Ftp.ListDirectory;
    
                StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
                request = null;
    
                string fileName = streamReader.ReadLine();
    
                while (fileName != null)
                {
                    FtpWebRequest requestFileDownload = null;
                    FtpWebResponse responseFileDownload = null;
    
                    try
                    {
                        Console.WriteLine(fileName);
    
                        requestFileDownload = (FtpWebRequest)WebRequest.Create(pathName + fileName);
                        requestFileDownload.Credentials = new NetworkCredential("LOGIN", "PASSWORD");
                        requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
                        responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();
    
                        Stream responseStream = responseFileDownload.GetResponseStream();
                        FileStream writeStream = new FileStream(localPath + fileName, FileMode.Create);
    
                       
    
                        int Length = 2048;
                        Byte[] buffer = new Byte[Length];
                        int bytesRead = responseStream.Read(buffer, 0, Length);
                        while (bytesRead > 0)
                        {
                            writeStream.Write(buffer, 0, bytesRead);
                            bytesRead = responseStream.Read(buffer, 0, Length);
    
                        }
                        
                        writeStream.Close();
    
                        Console.WriteLine("Download Done");
                    }
                    catch (System.Exception exceptionObj)
                    {
                        Console.WriteLine(exceptionObj.Message.ToString());
                    }
                    finally
                    {
                        requestFileDownload = null;
                        responseFileDownload = null;
                    }
    
                    fileName = streamReader.ReadLine();
                }
    
                request = null;
                streamReader = null;
    
            }
        }   
    }

    Monday, April 9, 2012 12:11 PM
  • I dont see any backgroundworker class inside your code. Try to implement it, then come back here if you will need any help.

    You can think of using Thread class (from Threading namespace) as well.

    What you have to do is: 

    - start a new thred (or DoWork method of bgv) 

    - count all the files

    - in a loop of all files then pass the progress value to progressBar.

    With bgv you can do it like:

    BackgroundWorker bgw = new BackgroundWorker();
    private void button1_Click(object sender, EventArgs e)
    {
        bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
        bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
        bgw.RunWorkerAsync();
    }
    void bgw_DoWork(object sender, DoWorkEventArgs e)
    {
        //get the total of files (number of them, or an array of them (like FileInfo[])
        for (i = 0; i < totalFiles.Length; i++)
        {
            //do what ever with the single file...
            //and pass the value to progressbar:
            bgw.ReportProgress(i);
        }
    }
    void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }

    Hope it helps a bit.

    bye


    Mitja

    Monday, April 9, 2012 12:21 PM
  • Use WebClient.DownloadFileAsync and update your ProgressBar in the DownloadProgressChanged event.
    • Proposed as answer by Rudedog2 Thursday, April 12, 2012 9:43 PM
    • Marked as answer by Bob Wu-MT Monday, April 30, 2012 3:03 AM
    Monday, April 9, 2012 6:19 PM
  •  

    I do not know how to proceed..
    And I do not know how the value of that class to which Form brings

    well i got this now: 
    Form1 and class1.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    //add form Form msdn
    using System.Threading;
    
    
    namespace WindowsFormsApplication1
    {
        
        public partial class Form1 : Form
        {
            public  string pathName;
            private BackgroundWorker bgw = new BackgroundWorker();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Class1.pathName=textBox1.Text;
                Class1.CombineBlocksIntoLibrary(Class1.getpathName());
    
                bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
                bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
                bgw.RunWorkerAsync();        
            }
    
    
            #region [ Threading ]
            private void bgw_DoWork(object sender, DoWorkEventArgs e)
            {        
               /* 
                //get the total of files (number of them, or an array of them (like FileInfo[])
                 for (i = 0; i < totalFiles; i++)
                {
                    //do what ever with the single file...
                    //and pass the value to progressbar:
                    bgw.ReportProgress(i);
                }*/
            }
            private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                // Reset the progress bar
                prbProgress.Minimum = 0;
                prbProgress.Value = 0;
                prbProgress.Enabled = false;
            }
            private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                 prbProgress.Value = e.ProgressPercentage;
            }
            #endregion
    
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        class Class1
        {
            public static String pathName;
    
    
            public static String getpathName()
            {
                return pathName;
            }
    
    
            public static void CombineBlocksIntoLibrary(string pathName)
            {
                string activeDir = @"c:\Temp";
    
                //Create a new subfolder under the current active folder
                string newPath = System.IO.Path.Combine(activeDir, "test");
                
                // Create the subfolder
                System.IO.Directory.CreateDirectory(newPath);
    
                //File Download,Write Local Copy, from FTP Location
                string localPath = @"C:\Temp\test\";
    
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(pathName);
                request.Credentials = new NetworkCredential("LOGIN", "PASSWORD");
                request.Method = WebRequestMethods.Ftp.ListDirectory;
    
                StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
                request = null;
    
                string fileName = streamReader.ReadLine();
    
                while (fileName != null)
                {
                    FtpWebRequest requestFileDownload = null;
                    FtpWebResponse responseFileDownload = null;
    
                    try
                    {
                        Console.WriteLine(fileName);
    
                        requestFileDownload = (FtpWebRequest)WebRequest.Create(pathName + fileName);
                        requestFileDownload.Credentials = new NetworkCredential("LOGIN", "PASSWORD");
                        requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
                        responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();
    
                        Stream responseStream = responseFileDownload.GetResponseStream();
                        FileStream writeStream = new FileStream(localPath + fileName, FileMode.Create);
    
                       
    
                        int Length = 2048;
    
                        Byte[] buffer = new Byte[Length];
                        int bytesRead = responseStream.Read(buffer, 0, Length);
                        while (bytesRead > 0)
                        {
                            writeStream.Write(buffer, 0, bytesRead);
                            bytesRead = responseStream.Read(buffer, 0, Length);
                        }
                        Int64 iRunningByteTotal = 0;
                        iRunningByteTotal += bytesRead;
                        
                        // calculate the progress out of a base "100"
                        double dIndex = (double)(iRunningByteTotal);
                        double dTotal = (double)buffer.Length;
                        double dProgressPercentage = (dIndex / dTotal);
                        int iProgressPercentage = (int)(dProgressPercentage * 100);
    
    
    
                        writeStream.Close();
    
                        Console.WriteLine("Download Done");
                    }
                    catch (System.Exception exceptionObj)
                    {
                        Console.WriteLine(exceptionObj.Message.ToString());
                    }
                    finally
                    {
                        requestFileDownload = null;
                        responseFileDownload = null;
                    }
    
                    fileName = streamReader.ReadLine();
                }
    
                request = null;
                streamReader = null;
    
            }
        }   
    }



    • Edited by Jacky871 Tuesday, April 10, 2012 10:30 AM
    Tuesday, April 10, 2012 10:29 AM
  • I will check it a bit later today.

    Mitja

    Wednesday, April 11, 2012 6:43 PM
  • Ok, i hope today :)
    • Edited by Jacky871 Friday, April 13, 2012 6:08 PM
    Thursday, April 12, 2012 7:36 PM
  • Use WebClient.DownloadFileAsync and update your ProgressBar in the DownloadProgressChanged event.

    For downloading files, would be better to use DownlaodFileAsync. Check here for a solution:

    http://www.dreamincode.net/forums/topic/115491-download-file-asynchronously-with-progressbar/


    Mitja

    • Marked as answer by Bob Wu-MT Monday, April 30, 2012 3:03 AM
    Saturday, April 14, 2012 12:17 AM