I am showing you how to use background worker to make an interesting stop watch.
Now create new window based application named as "StopWatchDemo", change the form name with "frmMain" and design it as shown in image below...
Now give name to the buttons sequentially as btnStart, btnStop, btnClose respectively.Give name "lblStopWatch" to label containing StopWatch. Give click events to each button. And here is complete code...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace StopWatchDemo
{
public partial class frmMain : Form
{
Stopwatch stopWatch = null;
BackgroundWorker backgroundWorker = null;
public frmMain()
{
InitializeComponent();
stopWatch = new Stopwatch();
backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.WorkerSupportsCancellation = true;
btnStop.Enabled = false;
}
void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
TimeSpan timeSpan = stopWatch.Elapsed;
lblStopWatch.Text = String.Format("Time : {0:00}:{1:00} sec", timeSpan.Minutes, timeSpan.Seconds);
}
void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
while(true)
{
backgroundWorker.ReportProgress(0);
System.Threading.Thread.Sleep(1000);
if (!stopWatch.IsRunning)
{
break;
}
}
}
private void btnStart_Click(object sender, EventArgs e)
{
backgroundWorker.RunWorkerAsync();
stopWatch.Start();
btnStart.Enabled = false;
btnStop.Enabled = true;
btnClose.Enabled = false;
}
private void btnStop_Click(object sender, EventArgs e)
{
backgroundWorker.CancelAsync();
stopWatch.Stop();
stopWatch.Reset();
btnStart.Enabled = true;
btnStop.Enabled = false;
btnClose.Enabled = true;
}
private void btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Now Start debugging, window will look like this as follows...
Now play with this....
This example shows how to use background worker facility given by .NET, it can be used in serious applications as I have used in some live projects...
Now create new window based application named as "StopWatchDemo", change the form name with "frmMain" and design it as shown in image below...
Now give name to the buttons sequentially as btnStart, btnStop, btnClose respectively.Give name "lblStopWatch" to label containing StopWatch. Give click events to each button. And here is complete code...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace StopWatchDemo
{
public partial class frmMain : Form
{
Stopwatch stopWatch = null;
BackgroundWorker backgroundWorker = null;
public frmMain()
{
InitializeComponent();
stopWatch = new Stopwatch();
backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.WorkerSupportsCancellation = true;
btnStop.Enabled = false;
}
void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
TimeSpan timeSpan = stopWatch.Elapsed;
lblStopWatch.Text = String.Format("Time : {0:00}:{1:00} sec", timeSpan.Minutes, timeSpan.Seconds);
}
void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
while(true)
{
backgroundWorker.ReportProgress(0);
System.Threading.Thread.Sleep(1000);
if (!stopWatch.IsRunning)
{
break;
}
}
}
private void btnStart_Click(object sender, EventArgs e)
{
backgroundWorker.RunWorkerAsync();
stopWatch.Start();
btnStart.Enabled = false;
btnStop.Enabled = true;
btnClose.Enabled = false;
}
private void btnStop_Click(object sender, EventArgs e)
{
backgroundWorker.CancelAsync();
stopWatch.Stop();
stopWatch.Reset();
btnStart.Enabled = true;
btnStop.Enabled = false;
btnClose.Enabled = true;
}
private void btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Now Start debugging, window will look like this as follows...
Now play with this....
This example shows how to use background worker facility given by .NET, it can be used in serious applications as I have used in some live projects...
No comments:
Post a Comment