Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have windows application for coping file with progress bar showing copied progress.But could not update progress bar.Its get updated when copy operation is get completed.

C#
public partial class FileCopy : Form
{
  private BackgroundWorker bw = new BackgroundWorker();
 public FileCopy()
       {
           InitializeComponent();
           bw.WorkerReportsProgress = true;
           bw.WorkerSupportsCancellation = true;
           bw.DoWork += new DoWorkEventHandler(bw_DoWork);
           bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
           bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

       }
 onstartButtonClick()
 {
   Thread readerThread = new Thread(new ThreadStart(readData));
   readerThread.Start();
   Thread writerThread = new Thread(new ThreadStart(writeData));
   writerThread.Start();
   writerThread.Join();
   readerThread.Join();
 }
 readData()
 {
   while (length > 0)
   {
    data = br.ReadBytes(data.Length);
    Buffer.putData(data);
    length = length - data.Length;
    Console.WriteLine("Reading {0}", data.Length);
    bw.ReportProgress(10);
   }
    Buffer.STOP = false;
    Buffer._reset.Set();
 }
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
       {
           this.BeginInvoke(new Action(() =>
           {
               Console.WriteLine("Inside Begin Invoke");
               progressBar1.Value = e.ProgressPercentage;
               Application.DoEvents();
           }));

       }
 public void writeData()
 {
    while (Buffer.STOP)
   {
     if (Buffer.STOP != false)
     {
          Buffer._reset.WaitOne();
          byte[] data = Buffer.takeData();
          if (data != null && Buffer.STOP == true)
          {
            fs.Write(data, 0, data.Length);
            fs.Flush();
          }
          Console.WriteLine("Writing to file :" + data.Length);
          Buffer._reset.Reset();
       }
   }
  }
}
 static class Buffer
   {
       public static  byte[] _data = new byte[10240];
       public static AutoResetEvent _reset = new AutoResetEvent(false);
       private static bool _isStop = true;

       public static bool STOP
       {
           get
           {
               return _isStop;
           }
           set
           {
               _isStop = value;
           }
       }
       public static void putData(byte[] data)
       {
           _data = data;
           Console.WriteLine("Data Length :" + data.Length);
           _reset.Set();
      }
       public static byte[] takeData()
       {
           return _data;
       }
   }

Progress is updated at end of operation.
Can you please suggest solution??
Posted

1 solution

You're generally on the right track with the idea of multi-threading in order to perform the file-copy and keeping your UI responsive. But your implementation is a bit far off ;)

Your code isn't showing what's happening in bw_DoWork(). The file-copying should take place there. You don't need the other two threads and the Buffer-class. That's overcomplicating things. Just have a loop that reads from one file a chunk of bytes and writes it to the new file until it's reached the end of the file.

You don't need an Invoke in the Eventhandler-Methods of the BackgroundWorker and you don't need an Application.DoEvents().

For examples of using a BackgroundWorker please take a look here:
http://www.dotnetperls.com/progressbar[^]
http://stackoverflow.com/a/1068743/4320056[^]
http://www.codeproject.com/Messages/5050414/Re-WinForms-Update-A-Dialog.aspx[^]
 
Share this answer
 
v2
Comments
Sarita S 7-May-15 5:53am    
There is requirement to have 2 thread for reading and writing.
And I did nothing() inside doWork.
Sascha Lefèvre 7-May-15 5:58am    
Is it a homework assignment to use 2 threads?
Sarita S 7-May-15 6:00am    
Yes.It is homework
Sarita S 7-May-15 6:00am    
To study about multithreading
Sascha Lefèvre 7-May-15 6:07am    
Okay. Do you have to use "new Thread(new ThreadStart(..))" or would two BackgroundWorkers also be ok for your homework?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900