none
循环体内多线程的问题 RRS feed

  • 问题

  •   foreach (Hashtable h in BalanceObjList)
                {
                                        new Thread(new ThreadStart(delegate()
                        {

                    string user = h["user"] as string;
                    string pass = h["pass"] as string;
                    string lei = h["lei"] as string;
                    HttpUtils.HttpWebRequest request = h["request"] as HttpUtils.HttpWebRequest;
                    if (lei == "tt")
                    {
                        ttGet(user, pass, request);
                    }
                    if (lei == "la")
                    {
                        la(user, pass, request);
                    }
                        })).Start();
                }

     

    --------------------------------------------------------------

    熟悉的人一看就应该知道这种方法在线程中只能获取最后的值,而无法得到预期遍历的效果,因为是初学者,不知道该如何修改,请大家指点下,不胜感激。

    2010年4月28日 4:50

答案

  • 你好,建議你將h作為參數傳遞

    我按照你的大體意思簡單設計了一個示例

     private static void TestThread()
            {
                List<MyTestClass> classes = new List<MyTestClass>();
                for (int i = 1; i < 40; i++)
                {
                    classes.Add(new MyTestClass(((char)i).ToString(), i));
                }

                foreach (MyTestClass cls in classes)
                {
                    Thread t = new Thread(DispClass);
                    //將MyTestClass實例作為參數傳入
                    t.Start(cls);
                }

                Console.ReadKey();
            }

            static void DispClass(object state)
            {
                MyTestClass cls = state as MyTestClass;
                if (null != cls)
                {
                    Console.WriteLine("String={0};Int={1};ThreadID={2}", cls.MyStr, cls.MyInt,Thread.CurrentThread.ManagedThreadId);
                }

    //如果有結果這裡還可以將結果保存在一個數組中
            }

            class MyTestClass
            {
                public string MyStr;
                public int MyInt;
                public MyTestClass(string str, int i)
                {
                    MyStr = str;
                    MyInt = i;
                }
            }

    運行結果如下:

    String=;Int=1;ThreadID=10
    String=;Int=2;ThreadID=11
    String=;Int=3;ThreadID=12
    String=;Int=4;ThreadID=13
    String=;Int=5;ThreadID=14
    String=;Int=6;ThreadID=15
    String=;Int=7;ThreadID=16
    String;Int=8;ThreadID=17
    String= ;Int=9;ThreadID=18
    String=
    ;Int=10;ThreadID=19
    String= ;Int=12;ThreadID=21
    String=;Int=14;ThreadID=23
    String=;Int=16;ThreadID=25
    String=;Int=18;ThreadID=27
    String=;Int=20;ThreadID=29
    String=;Int=22;ThreadID=31
    String=;Int=24;ThreadID=33
    String=;Int=26;ThreadID=35
    String=;Int=28;ThreadID=37
    String=;Int=30;ThreadID=39
    String= ;Int=32;ThreadID=41
    String= ;Int=11;ThreadID=20
    ;Int=13;ThreadID=22
    String=;Int=15;ThreadID=24
    String=;Int=17;ThreadID=26
    String=;Int=19;ThreadID=28
    String=;Int=21;ThreadID=30
    String=;Int=23;ThreadID=32
    String=;Int=25;ThreadID=34
    String=;Int=27;ThreadID=36
    String=;Int=29;ThreadID=38
    String=;Int=31;ThreadID=40
    String=!;Int=33;ThreadID=42
    String=";Int=34;ThreadID=43
    String=#;Int=35;ThreadID=44
    String=$;Int=36;ThreadID=45
    String=%;Int=37;ThreadID=46
    String=&;Int=38;ThreadID=47
    String=';Int=39;ThreadID=48

     


    I see you~http://hi.baidu.com/1987raymondMy Blog~~~
    2010年4月30日 1:38
    版主