PDA

View Full Version : تا زمانی که تابع در حال اجراست



msasan1367
دوشنبه 13 تیر 1390, 16:02 عصر
سلام
یک تابع داریم و یک دکمه که با زدن دکمه تابع اجرا می شود. حال می خواهیم شرطی ایجاد کنیم که تا زمانی که تابع در حال اجراست دکمه غیرفعال شود و با پایان رسیدن اجرای تابع دکمه دوباره فعال شود
لطفاً راهنمایی فرمایید
با تشکر

r00tkit
دوشنبه 13 تیر 1390, 16:20 عصر
اول تابع دکمه رو غیر فعال و اخر تابع فعالش کن :)))))))))))))))))))))))))

msasan1367
دوشنبه 13 تیر 1390, 19:09 عصر
اما با این کار یک لحظه دکمه غیرفعال و سپس فعال می شود. این هم کد:

private void Sort_Click(object sender, EventArgs e)
{
Set.Enabled = false;
int speed = 100 - Speed.Value;

SortAlgorithm sa1 = new SortAlgorithm(array1, pnlSort1, speed);
SortAlgorithm sa2 = new SortAlgorithm(array2, pnlSort2, speed);
SortAlgorithm sa3 = new SortAlgorithm(array3, pnlSort3, speed);
SortAlgorithm sa4 = new SortAlgorithm(array4, pnlSort4, speed);
SortAlgorithm sa5 = new SortAlgorithm(array5, pnlSort5, speed);

ThreadStart ts = delegate()
{
try
{
System.Diagnostics.Stopwatch T = new System.Diagnostics.Stopwatch();
T.Start();
sa1.BubbleSort(array1);
T.Stop();
Invoke(new MethodInvoker(delegate { Timerlbl1.Text = T.ElapsedMilliseconds.ToString(); }));
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
};

ThreadStart ts2 = delegate()
{
try
{
System.Diagnostics.Stopwatch T = new System.Diagnostics.Stopwatch();
T.Start();
sa2.InsertionSort(array2);
T.Stop();
Invoke(new MethodInvoker(delegate { Timerlbl2.Text = T.ElapsedMilliseconds.ToString(); }));
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
};
ThreadStart ts3 = delegate()
{
try
{
System.Diagnostics.Stopwatch T = new System.Diagnostics.Stopwatch();
T.Start();
sa3.SelectionSort(array3);
T.Stop();
Invoke(new MethodInvoker(delegate { Timerlbl3.Text = T.ElapsedMilliseconds.ToString(); }));
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
};
ThreadStart ts4 = delegate()
{
try
{
System.Diagnostics.Stopwatch T = new System.Diagnostics.Stopwatch();
T.Start();
sa4.QuickSort(array4, 0, array4.Count - 1);
T.Stop();
Invoke(new MethodInvoker(delegate { Timerlbl4.Text = T.ElapsedMilliseconds.ToString(); }));
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
};
ThreadStart ts5 = delegate()
{
try
{
System.Diagnostics.Stopwatch T = new System.Diagnostics.Stopwatch();
T.Start();
sa5.MergeSort(array5, 0, array5.Count - 1);
T.Stop();
Invoke(new MethodInvoker(delegate { Timerlbl5.Text = T.ElapsedMilliseconds.ToString(); }));
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
};
Thread t = new Thread(ts);
t.Start();

Thread t2 = new Thread(ts2);
t2.Start();

Thread t3 = new Thread(ts3);
t3.Start();

Thread t4 = new Thread(ts4);
t4.Start();

Thread t5 = new Thread(ts5);
t5.Start();
Set.Enabled = true;
}