多线程的操作包括带参数的和不带参数的

 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);
    }
}

第二种:


不带参数的 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() { }
posted @ 2011-10-08 10:15  星月磊子  阅读(312)  评论(0编辑  收藏  举报