多线程的操作包括带参数的和不带参数的
string[] urlcp = GetPage(curl);
Thread[] mythread = new Thread[urlcp.Length];
for (int i = 0; i < urlcp.Length; i++)
{
//getcontent(curl);
getcontent(urlcp[i]);
object[] urcp1 = (object[])urlcp;
mythread[i] = new Thread(new ParameterizedThreadStart(getcontent1));
mythread[i].Start(urcp1);//传递参数
}
带object参数的ParameterizedThreadStart:
public void Start()
{
for (int index = 0; index < 10; index++)
{
#region -- 准备参数 --
List<int> list = new List<int>();
for (int param = index; param < index + 10; param++)
{
list.Add(param);
}
#endregion
Thread thread = new Thread(new ParameterizedThreadStart(InitThreadFunc));
thread.Start(list);
}
LogHelper.WriteLine("Over");
}
/// <summary>
/// 执行的函数
/// </summary>
/// <param name="list"></param>
private void InitThreadFunc(object list)
{
if (list is List<int>)
{
List<int> ll = list as List<int>;
int result = 0;
foreach (int n in ll)
{
result += n;
}
LogHelper.WriteLine(result);
}
}
{
for (int index = 0; index < 10; index++)
{
#region -- 准备参数 --
List<int> list = new List<int>();
for (int param = index; param < index + 10; param++)
{
list.Add(param);
}
#endregion
Thread thread = new Thread(new ParameterizedThreadStart(InitThreadFunc));
thread.Start(list);
}
LogHelper.WriteLine("Over");
}
/// <summary>
/// 执行的函数
/// </summary>
/// <param name="list"></param>
private void InitThreadFunc(object list)
{
if (list is List<int>)
{
List<int> ll = list as List<int>;
int result = 0;
foreach (int n in ll)
{
result += n;
}
LogHelper.WriteLine(result);
}
}
第二种:
不带参数的 InitThreadFunc
public void Start()
{
for (int index = 0; index < 10; index++)
{
Thread thread = new Thread(new ThreadStart(InitThreadFunc));
thread.Start();
}
LogHelper.WriteLine("Over");
}
private void InitThreadFunc() { }
{
for (int index = 0; index < 10; index++)
{
Thread thread = new Thread(new ThreadStart(InitThreadFunc));
thread.Start();
}
LogHelper.WriteLine("Over");
}
private void InitThreadFunc() { }