Skip to content

Instantly share code, notes, and snippets.

@GiovanniMounir
Created October 24, 2012 00:31
Show Gist options
  • Save GiovanniMounir/3942982 to your computer and use it in GitHub Desktop.
Save GiovanniMounir/3942982 to your computer and use it in GitHub Desktop.
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;
using System.Threading;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Stopwatch _timer = new Stopwatch();
private int _total = 100;
private int _current = 0;
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
_current = 0;
_timer.Reset();
_timer.Start();
for (int i = 0; i <= _total; i++) // It's <= not <. You might be missing this
{
backgroundWorker1.ReportProgress(i);
Thread.Sleep(500);
_current++;
}
_timer.Stop();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Maximum = _total;
progressBar1.Value = e.ProgressPercentage;
if (_timer.IsRunning && _current > 0)
{
float elapsedMin = ((float)_timer.ElapsedMilliseconds / 1000) / 60;
float minLeft = (elapsedMin / _current) * (_total - _current);
TimeSpan eta = TimeSpan.FromMinutes(minLeft);
label1.Text = eta.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment