关于主线程等待子线程结束
请问这段程序如何让主线程等待子线程运行结束后执行主线程使用Thread.join就会导致程序崩溃
--------------------编程问答-------------------- 工作线程(子线程)结束后进行回调处理。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace Test2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
delegate void wt();
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(f));
t.Start();
//t.Join();
MessageBox.Show("主线程");
}
void f()
{
wtf();
}
void wtf()
{
if (this.InvokeRequired)
{
this.Invoke(new wt(wtf));
}
else
{
this.label1.Text = "Yes";
}
}
}
}
为什么主线程要等待,不明白。 --------------------编程问答--------------------
因为我需要等待子线程结束后会触发一个事件,然后使主线程继续执行后面的代码。
如果按照你说的那样回调 请问该如何回调呢? --------------------编程问答-------------------- 使用BeginInvoke回调,在Completed事件中编写结束后处理的代码。 --------------------编程问答--------------------
大虾 还是不明白如何回调啊!
麻烦给点例子我看下吧.
补充:.NET技术 , C#