线 [英] thread

查看:75
本文介绍了线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如何等到线程完成作业后再继续使用

原始帖子?

public void main(string [] args)

{

Thread t = new Thread(new ThreadStart(DoWork));

t.Start();


while(t.IsAlive)

{

//我不是知道如何等待线程t完成

//然后执行下面的MessageBox。我不想要

//一个空白的循环体,因为它占用了100%

//我的CPU

}


MessageBox.Show(" Bla");

}


private void DoWork()

{

//做更多

}


请指教,谢谢!

-P

Hi,

How do I wait until a thread is finished his job then continue to the
original thread?

public void main(string[] args)
{
Thread t = new Thread(new ThreadStart(DoWork));
t.Start();

while(t.IsAlive)
{
// I don''t know how to wait until thread t is done
// then execute MessageBox below. I don''t want
// a blank while loop body because it takes up 100%
// of my CPU
}

MessageBox.Show("Bla");
}

private void DoWork()
{
// do more
}

Please advice, thanks!
-P

推荐答案

t.Start();

t.Join();

//继续....


威利。


" Paul" <帕** @ discussions.microsoft.com>在消息中写道

news:32 ********************************** @ microsof t.com ...
t.Start();
t.Join();
// Go on ....

Willy.

"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...


我怎么等到一个帖子完成他的工作然后继续到原来的线程?

public void main(string [] args)
{
线程t =新线程(新的ThreadStart(DoWork));
t.Start();
<同时(t.IsAlive)
//
//我不知道如何等待线程完成
//然后执行下面的MessageBox。我不想要
//一个空白的循环体,因为它占用我的CPU的100%
}

MessageBox.Show(& ; Bla");
}

私有无效DoWork()
{
//做更多
}

请建议,谢谢!
-P
Hi,

How do I wait until a thread is finished his job then continue to the
original thread?

public void main(string[] args)
{
Thread t = new Thread(new ThreadStart(DoWork));
t.Start();

while(t.IsAlive)
{
// I don''t know how to wait until thread t is done
// then execute MessageBox below. I don''t want
// a blank while loop body because it takes up 100%
// of my CPU
}

MessageBox.Show("Bla");
}

private void DoWork()
{
// do more
}

Please advice, thanks!
-P



Willy Denoyette [MVP]< wi ************* @ pandora.be>写道:
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
t.Start();
t.Join();
//继续....
t.Start();
t.Join();
// Go on ....




然后你需要问问自己为什么你想要在

中获得第一名的另一个帖子。如果你要启动一个线程然后阻止

当前线程直到新线程完成,为什么不直接在原始线程中调用

方法?


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件



And then you need to ask yourself why you want another thread in the
first place. If you''re going to start a thread and then block the
current thread until the new thread has finished, why not just call the
method directly in the original thread?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Jon,


我不必问自己,OP应该问: - )。

我看到很多人在今天尝试使用线程,可能要学习它们是如何工作的,并且希望发现有很多可以做的事情

没有他们。


也许更好的答案就是......


t.Start() ;

//如果你有并行运行的东西,现在就做,否则不要使用

线程并在这里调用你的程序。

//最后,如果你是eed线程程序的结果或者你只是简单地想要确保线程程序完成它的工作

//调用:

t.Join();


Willy。


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
Jon,

I don''t have to ask myself, OP should ask :-).
I see many people experimenting with threads these day''s, probably to learn
how they work and hopefuly to discover that there is a lot that can be done
without them.

Maybe a better answer would have been...

t.Start();
// if you have something to run in parallel, do it now, else don''t use a
thread and call your procedure here.
// finally, If you need the outcome of the thread procedure or you simply
want to be sure the thread procedure finished it''s job
// call:
t.Join();

Willy.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Willy Denoyette [MVP]< wi ************* @ pandora.be>写道:
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
t.Start();
t.Join();
//继续....
t.Start();
t.Join();
// Go on ....



然后你需要问问自己为什么你想要第一个位置的另一个线程。如果您要启动一个线程,然后阻止当前线程直到新线程完成,为什么不直接在原始线程中调用
方法?
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复该群组,请不要给我发邮件



And then you need to ask yourself why you want another thread in the
first place. If you''re going to start a thread and then block the
current thread until the new thread has finished, why not just call the
method directly in the original thread?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



这篇关于线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆