0
    private void btnUpdate_Click(object sender, EventArgs e)
    {                 
      bgWorker.RunWorkerAsync();                                
    }

    private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
    {          
        BackUpDatabase.BackUp(this.txtPath.Text);
        for (int i = 0; i <= 100; i++)
        {
            // Report progress to 'UI' thread
            bgWorker.ReportProgress(i);
            // Simulate long task
            System.Threading.Thread.Sleep(100);
        }
    }

    private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        lblProgress.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
    }

the code is working but i doubt that the progress is not the exact time process. am i missing something here?

RJ Lohan
  • 6,497
  • 3
  • 34
  • 54
athan
  • 11
  • 3
  • You need to state what it is that you are expecting to achieve here. It is not obvious – RJ Lohan Jun 17 '13 at 03:53
  • What do you think is wrong with this code? – xxbbcc Jun 17 '13 at 03:54
  • i want to see the progressbar1 display the exact time process of backing-up my database sir.tnx – athan Jun 17 '13 at 03:56
  • Does `BackUpDatabase.BackUp();` give you any kind of "progress" update type events? Is this written by you?... – Idle_Mind Jun 17 '13 at 03:59
  • BackUpDatabase.BackUp();yes i wrote it sir. it is the process in saving my database sir..i want to get the exact "time process" of BackUpDatabase.BackUp() i've wrote. – athan Jun 17 '13 at 04:05
  • It really sounds like you want two background workers, one to do the actual backup, and one to be counting how much time it's taking – m.t.bennett Jun 17 '13 at 05:01

1 Answers1

0

Your question is a bit vague, however ill give it a crack:

It sounds like to me you need two background workers, one to do the actual backup, and one to report the time in real-time.

Luckily there is a timer that runs in a background thread! System.Timers.Timer.

You already have one thread setup to do the work, so all you have to do is create your timer, and start/update/stop it.

Heres how the code might work:

private Timer _Timer;
private DateTime _Start;
private void InitTimer()
{
    _Start = DateTime.Now;
    _Timer = new Timer(100);
    _Timer.AutoReset = false;
    _Timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
    _Timer.Start();
}
private void Timer_Elapsed(object sender, ElapsedEventArgs  e)
{
    TimeSpan diff = DateTime.Now - _Start;
    this.Invoke(new MethodInvoker(delegate()
    {
        lblProgress.Text = String.Format("Time: {0}", diff);
    }));
}
//starts the worker thread
private void btnUpdate_Click(object sender, EventArgs e)
{
    InitTimer();
    bgWorker.RunWorkerAsync();
}
//Does the backup
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
    BackUpDatabase.BackUp(this.txtPath.Text);
}
//Stops the timer when the backup finishes
private void bgWorker_WorkCompleted(object sender, RunWorkerCompletedEventArgs e)
{

    _Timer.Stop();
}

I haven't tested this code and I maybe way off base with this :) but enjoy :)

EDIT:

Just thinking about this some more, if the OP want's to get a percentage He will need to break his backups into steps and report it manually.

m.t.bennett
  • 1,290
  • 16
  • 34