Created
October 24, 2012 00:31
-
-
Save GiovanniMounir/3942982 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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