如何中止线程Tcp连接

时间:2016-04-15 20:31:07

标签: c# multithreading

我开始学习使用成功的线程,但我不能中止线程 程序无法成功结束。

请帮助我如何中止?

for (int i = 0; i < dtProd.Rows.Count; i++)
            {
                sayacno = dtProd.Rows[i].ItemArray[2].ToString();
                komut = "<25></25>";
                Connection TCP = new Connection("as","sd","fd","gd");
                Thread t = new Thread(new ThreadStart(TCP.Bagla));
                t.IsBackground = true;
                t.Start();
                threads.Add(t);
                Thread.Sleep(100);
            }

            for (int t = 0; t < threads.Count; t++)
            {
                try
                {
                    threads[t].Join();
                    threads[t].Abort();
                }
                catch (ThreadAbortException tx)
                {
                    Console.Write(tx.ToString());
                }            
            }

1 个答案:

答案 0 :(得分:0)

我认为你的改变来自:

   threads[t].Join();
   threads[t].Abort();

为:

   threads[t].Abort();
   threads[t].Join();