[C#] 纯文本查看 复制代码
private void btnExec_Click(objectsender, EventArgs e)[/align] {
List<Users>ulist = new List<Users>();
ulist.Add(new Users("君临0", "123456"));
ulist.Add(new Users("君临1", "123456"));
ulist.Add(new Users("君临2", "123456"));
ulist.Add(new Users("君临3", "123456"));
//多线程1. 一条线程执行多任务.
Thread th = newThread(new ThreadStart(delegate{
for (inti = 0; i < ulist.Count; i++)
{
lock(obj)
{
x =GetIndexHtml(ulist);
//x 得到值以后才能继续
}
}
}));
th.Start();
}
object obj = new object();
string data = "{0},{1}";
string url = "www.msdn5.com";
int x;//公共变量
private intGetIndexHtml(Users user)
{
/*
get 过程
*/
string str = string.Format(data,user.Name, user.Pass);
return 0;
}
//多线程2
for (inti = 0; i < ulist.Count; i++)
{
//多少个任务多少条线程.
Thread th = newThread(new ThreadStart(delegate
{
lock(obj) //这还有用么?
{
x =GetIndexHtml(ulist);
//x 得到值以后才能继续
}
}));
th.Start();
}
//多线程3
for (inti = 0; i < ulist.Count; i++)
{
lock (obj) //这是可以的
{
//多少个任务多少条线程.
Threadth = new Thread(new ThreadStart(delegate
{
x =GetIndexHtml(ulist);
//x 得到值以后才能继续
}));
th.Start();
}
}