多线程,如何判断所有子线程都已经运行完毕(结束)

emanlee 2007-08-23 06:17:12
下面开了多个线程,如何判断所有子线程都已经运行完毕?
Dim i As Integer = 0
For i = 0 To item_count
tmpEmail = lstAddress.Items(i).Text
tmpEmail = Trim(Left(tmpEmail, 50))
Dim start As System.Threading.ParameterizedThreadStart
start = New ParameterizedThreadStart(AddressOf SendEmail)
Dim t As Thread = New Thread(start)
t.Start(tmpEmail)

Next
开始运行前提示“正在**请稍候**”,所有线程结束之后,提示“完成。”
...全文
1119 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wz_hzb 2008-07-20
  • 打赏
  • 举报
回复
CheckIp *checkrun[10];
for (int j=0;j<10 && !q1->Eof;j++)
{
MainForm->Memo2->Lines->Add(DateTimeToStr(Date()+Time())+" 检测 "+q1->FieldByName("ip")->AsString);
//MainForm->Label3->Caption=IntToStr(MainForm->Label3->Caption.ToInt()+1);
checkrun[j]=new CheckIp(false);
checkrun[j]->FreeOnTerminate=false;
checkrun[j]->ip=q1->FieldByName("ip")->AsString;
checkrun[j]->port=q1->FieldByName("port")->AsString.ToInt();
checkrun[j]->Resume();
q1->MoveBy(1);
}
for (;checkrun[0]->islive || checkrun[1]->islive || checkrun[2]->islive || checkrun[3]->islive || checkrun[4]->islive || checkrun[5]->islive || checkrun[6]->islive || checkrun[7]->islive || checkrun[8]->islive || checkrun[9]->islive;)
Sleep(1000);
delete checkrun;


__fastcall CheckIp::CheckIp(bool CreateSuspended)
: TThread(CreateSuspended)
{
islive=true;
}
//---------------------------------------------------------------------------
void __fastcall CheckIp::Execute()
{
//---- Place thread code here ----
//我的程序代码.....
islive=false;
return;
}

yuzl32 2007-08-25
  • 打赏
  • 举报
回复
采用WaitHandle等待所有线程执行完毕!

using System;
using System.Threading;

public sealed class App
{
// Define an array with two AutoResetEvent WaitHandles.
static WaitHandle[] waitHandles = new WaitHandle[]
{
new AutoResetEvent(false),
new AutoResetEvent(false)
};

// Define a random number generator for testing.
static Random r = new Random();

static void Main()
{

DateTime dt = DateTime.Now;
Console.WriteLine("Main thread is waiting for BOTH tasks to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
WaitHandle.WaitAll(waitHandles);

Console.WriteLine("Both tasks are completed (time waited={0})",
(DateTime.Now - dt).TotalMilliseconds);

// Queue up two tasks on two different threads;
// wait until any tasks are completed.
dt = DateTime.Now;
Console.WriteLine();
Console.WriteLine("The main thread is waiting for either task to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
int index = WaitHandle.WaitAny(waitHandles);
// The time shown below should match the shortest task.
Console.WriteLine("Task {0} finished first (time waited={1}).",
index + 1, (DateTime.Now - dt).TotalMilliseconds);
}

static void DoTask(Object state)
{
AutoResetEvent are = (AutoResetEvent) state;
int time = 1000 * r.Next(2, 10);
Console.WriteLine("Performing a task for {0} milliseconds.", time);
Thread.Sleep(time);
are.Set();
}
}
重典 2007-08-25
  • 打赏
  • 举报
回复
我是在类中设一个Dictionary然后在线程函数中写状态进Dictionary在主函数中读。。。都是某状态就OK 了
platform 2007-08-24
  • 打赏
  • 举报
回复
myThread mThread = new myThread();
try
{
Console.Title = "博客搬家后台服务 --Sean Pu 2007.08.23)";
Parse.Cl("博客搬家后台服务开始运行...", ConsoleColor.DarkGreen);
mThread.MainThread(); //Start MainThread and MainThread start some child threads
XXX
mThread.TerminateThread();//terminate child threads
Parse.Cl("现在开始停止所有服务,请稍等片刻....");
while (myThread.ThreadNum != 0)
{
System.Threading.Thread.Sleep(100);
}
Console.Beep();
Parse.Cl("博客搬家后台服务停止完成.");
Parse.Cl("Press any key to continue... ...");
Console.ReadKey();
}
platform 2007-08-24
  • 打赏
  • 举报
回复
1、写一个循环
2、给一个线程计数器;
3、给一个结束线程的方法;
4、循环等待技术为0
tianxiaoku 2007-08-24
  • 打赏
  • 举报
回复
that's not a bad question!
emanlee 2007-08-24
  • 打赏
  • 举报
回复
up
wei123456 2007-08-23
  • 打赏
  • 举报
回复
If t.ThreadState = ThreadState.Stopped Then
'完成
EndIf

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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