如何设置线程完全结束后运行其他内容?

文盲老顾
WEB应用领新星创作者
博客专家认证
2012-08-07 10:00:40

public delegate void delegateHandle(object sender,EventArgs e);

public partial class FCPMClient : Form
{
public event delegateHandle onFinished;
private Thread thread = null;

public FCPMClient()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(fun1));
thread.Start();
this.onFinished += new delegateHandle(fun2);
}

private void fun1(){
onFinished(this, new EventArgs());
}

private void fun2(object sender, EventArgs e){
thread.Abort();
}
}


我现在的需求是在某一个线程结束后运行某些代码,但我的程序类似上边这些,调用 fun2 时,实际上线程还在进行,并没有完全结束,这个要怎么解决?
...全文
260 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
bbjiabcd 2012-08-13
  • 打赏
  • 举报
回复
private Thread thread = null;这句没用,去掉
 
public delegate void delegateHandle();

public partial class FCPMClient : Form
{
public event delegateHandle onFinished;

public FCPMClient()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(fun1));
thread.Start();
label1.Text = "线程开始";
}

private void fun1(){
Thread.Sleep(3000);
onFinished mi = new onFinished(fun2);
}

private void fun2(){
label1.Text = "线程结束";
}
}
bbjiabcd 2012-08-13
  • 打赏
  • 举报
回复
[code=c#]
public delegate void delegateHandle();

public partial class FCPMClient : Form
{
public event delegateHandle onFinished;
private Thread thread = null;

public FCPMClient()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(fun1));
thread.Start();
label1.Text = "线程开始";
}

private void fun1(){
Thread.Sleep(3000);
onFinished mi = new onFinished(fun2);
}

private void fun2(){
label1.Text = "线程结束";
}
}
[/code]
lyso2008 2012-08-13
  • 打赏
  • 举报
回复
看你想怎么用多线程序:
1.如果是compute bound,用Task.ContinueWith或TaskFactory.ContinueXXXXXX
(1).参考:http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.continuewith.aspx
(2).http://msdn.microsoft.com/en-us/library/dd321446.aspx
2.如果是用BeginXXX,EndXXX,就用callback.
wushuai1346 2012-08-08
  • 打赏
  • 举报
回复
有没有试过 Task?这个也是多线程的,感觉比Thread好用.

Task<Int32> t = new Task<Int32>(n => Sum((Int32)n),100000000);
t.Start();
Task cwt = t.ContinueWith(task = > Console.WriteLine("The sum is: " + task.Result));
文盲老顾 2012-08-08
  • 打赏
  • 举报
回复
to 7L:

感谢楼上的,不过这个方法不是我所希望的,我在程序内加上类似的代码,程序也是挂起的

因为在 fun1 内有很多处理过程,只有发生特定情况时,才会终止线程
yezhendong185 2012-08-07
  • 打赏
  • 举报
回复
按1L的做法就行了吧!

private Thread thread = null;

private void Form1_Load(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(fun1));
thread.Start();
this.onFinished += new delegateHandle(fun2);
}

private void fun1()
{
onFinished(this, new EventArgs());
}

private void fun2(object sender, EventArgs e)
{
thread.Join();
thread.Abort();
}
文盲老顾 2012-08-07
  • 打赏
  • 举报
回复
我是使用 Timer 组件定时检查,当 thread.IsAlive 的值是 false 时,确认线程完全结束,除了这个,有没有其他方法?
SocketUpEx 2012-08-07
  • 打赏
  • 举报
回复
在fun1最后通知程序执行fun2


文盲老顾 2012-08-07
  • 打赏
  • 举报
回复
问题是,fun2 就是触发后运行的内容,但是,这个是在线程内触发的,在执行 fun2 时,thread 并没有完全结束
bdmh 2012-08-07
  • 打赏
  • 举报
回复
thread.Join或者用其他方式,比如event触发,委托触发等
superliu1122 2012-08-07
  • 打赏
  • 举报
回复


public partial class FCPMClient : Form
{
EventWaitHandle eventWaitHandle = new ManualResetEvent(false);
private Thread thread = null;

public FCPMClient()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(fun1));
thread.Start();
this.eventWaitHandle.WaitOne();
fun2();
}

private void fun1(){
this.eventWaitHandle.Set();
}

private void fun2(object sender, EventArgs e){

}
}
文盲老顾 2012-08-07
  • 打赏
  • 举报
回复
to 5L:

这么搞,程序直接挂起了

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧