|
namespace
BackgroundWorker
{
public
partial
class
frmMain
: Form
{
public
bool
FLAG = true;
public
long MAX
= 200000000;
public
frmMain()
{
InitializeComponent();
}
private
void
backgroundWorker1_DoWork(object
sender,
DoWorkEventArgs e)
{
int
i = 1;
for
(long l
= 0; l < MAX; l++)
{
if (l == (long)(MAX
/ 100) * i)
{
backgroundWorker1.ReportProgress(i);
i++;
}
if
(backgroundWorker1.CancellationPending)
{
e.Cancel =
true;
return;
}
}
}
private
void
backgroundWorker1_ProgressChanged(object
sender,
ProgressChangedEventArgs e)
{
prgsBar.Value =
e.ProgressPercentage;
lblReport.Text =
"شمارش
در
حال
انجام
است
لطفا
بردباری
کنید "
+ "( "
+ e.ProgressPercentage.ToString() +
"
درصد"
+ " ) ";
this.Update();
}
private
void
backgroundWorker1_RunWorkerCompleted(object
sender,
RunWorkerCompletedEventArgs e)
{
lblReport.Visible =
false;
prgsBar.Visible =
false;
btnCountStop.Text =
"شمارش";
FLAG =
true;
}
private
void
btnMsg_Click(object
sender,
EventArgs e)
{
MessageBox.Show("برنامه
به
درخواست
کاربر
پاسخ
می
دهد",
"نمایش
پیام",
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk);
}
private
void
btnCountStop_Click(object sender, EventArgs e)
{
if
(FLAG)
{
lblReport.Visible =
true;
prgsBar.Visible =
true;
btnCountStop.Text =
"انصراف";
toolTip1.SetToolTip(this.btnCountStop,
"Cancel");
backgroundWorker1.RunWorkerAsync();
FLAG =
false;
}
else
{
lblReport.Visible =
false;
prgsBar.Visible =
false;
btnCountStop.Text =
"شمارش";
toolTip1.SetToolTip(this.btnCountStop,
"Counting");
backgroundWorker1.CancelAsync();
FLAG =
true;
}
}
private
void
btnExit_Click(object
sender,
EventArgs e)
{
this.Close();
}
}
} |