读书人

100分高分~关于判断多线程是否结束

发布时间: 2013-01-12 16:25:03 作者: rapoo

100分高分求助~!关于判断多线程是否结束
高分求助~~
小弟开启了100个进程,想让进程所有的进程都结束后转到“showhtml”页面。现在我不会做判断进程是否全部结束~



protected void search_Click(object sender, EventArgs e)
{
int Start = 0;
string question = TextBox1.Text.ToString();
if (question.Equals(""))
{
Response.Write("请输入您要查询的内容!");

}
else
{
Response.Write("正在查询并下载网页...");
Thread[] myThread;//声名下载线程
int n = 1;//设置线程总数,同时规定下载的连接数 此处为1000,即下载1000个链接
myThread = new Thread[ n ];//为线程申请资源,确定线程总数
for( int i = 0; i < n; i++ )
{
Start = i * 10;
string gurl = "http://www.google.com/search?q=" + System.Web.HttpUtility.UrlEncode(question) + "&start=" + Start;//按照UTF8编码
imple myimpl = new imple(gurl, question,Start);
ThreadStart startDownload = new ThreadStart(myimpl.getgoogle);
myThread[i] = new Thread( startDownload );//指定线程起始设置
myThread[i].Start();//逐个开启线程
//判断线程是不是结束~

int time = new Random().Next(1000);//休眠1000毫秒内的随机数
Thread.Sleep(time);

}
//while (IsAlive)???该怎么写呢?
//{ }
}

}

很急~~谢谢大侠们了
[解决办法]

int num = 0, num1 = 0, num2 = 0, num3 = 0;

System.Threading.Thread thread1 = new System.Threading.Thread(delegate()


{
System.Threading.Thread.Sleep(5000);
num1 = 10;
});
thread1.Start();


System.Threading.Thread thread2 = new System.Threading.Thread(delegate()
{
System.Threading.Thread.Sleep(5000);
num2 = 100;
});


thread2.Start();

System.Threading.Thread thread3 = new System.Threading.Thread(delegate()
{
System.Threading.Thread.Sleep(10000);
num3 = 8;
});
thread3.Start();



while (true)
{
if (thread1.ThreadState != System.Threading.ThreadState.Stopped) continue;
if (thread2.ThreadState != System.Threading.ThreadState.Stopped) continue;
if (thread3.ThreadState != System.Threading.ThreadState.Stopped) continue;

break;
}


num = num1 + num2 + num3;

Console.WriteLine(num);


[解决办法]
new Thread((ThreadStart)delegate { A(); }).Start();
if(t[i].ThreadState!=ThreadState.Stopped)
System.Windows.Forms.Application.DoEvents();

[解决办法]

while(CheckIsStop(myThread,n))
{
Response.Redirect("show.html");


}


public bool CheckIsStop(Thread[] myThread, int n)
{
for( int i = 0; i < n; i++ )
{
if(myThread[i].ThreadState!=ThreadState.Aborted)
{
return false;
}
}
return true;
}



[解决办法]
上面逻辑不对,重写了下

bool isStop=false;
while(!isStop)
{
isStop=CheckIsStop(myThread,n);
}
Response.Redirect("show.html");



public bool CheckIsStop(Thread[] myThread, int n)
{
for( int i = 0; i < n; i++ )
{
if(myThread[i].ThreadState!=ThreadState.Aborted)
{
return false;
}
}
return true;
}


[解决办法]
引用:
C# code

int num = 0, num1 = 0, num2 = 0, num3 = 0;

System.Threading.Thread thread1 = new System.Threading.Thread(delegate()
……

up


while(true)
{
if(thread1.ThreadState!=Sytem.Threading.ThreadState.Stopped) continus;
}

读书人网 >C#

热点推荐