110,499
社区成员
发帖
与我相关
我的任务
分享
private void Form1_Load(object sender, EventArgs e)
{
Thread thread1 = new Thread(new ThreadStart(Do1));
thread1.Start();
Thread thread2 = new Thread(new ThreadStart(Do2));
thread2.Start();
}
private void Do1()
{
}
private void Do2()
{
}
private bool StartThreads()
{
bool IfTimesEnd = false;
IfRunOver = false;
Thread newThread = new Thread(new ThreadStart(ThreadProc));
newThread.Start();
while (!IfRunOver)
{
IfTimesEnd = newThread.IsAlive;
System.Windows.Forms.Application.DoEvents();
if (!IfTimesEnd || IfRunOver)
{
newThread.Interrupt();
newThread.Abort();
IfTimesEnd = false;
break;
}
}
return !IfTimesEnd;
}
//而主线程中的相关代码为:
while (true)
{
IfTimesEnd = StartThreads();
IfThreadOver = IfTimesEnd && true;
System.Windows.Forms.Application.DoEvents();
if (IfRunOver && IfThreadOver)
{
pt.Stop();
str = "用时:" + pt.Duration + " 秒\n";
richTextBox1.ScrollToCaret();
richTextBox1.AppendText(str);
richTextBox1.Update();
break;
}
}
var callback = new Action(() => Console.WriteLine("我结束了"));
Thread thread1 = new Thread(new ParameterizedThreadStart(Do1));
thread1.Start(callback);
private void Do1(object arg)
{
// do something
((Action)arg).Invoke();
}