Thread(线程)

Thread开启线程:接收一个参数

TestClass tc = new TestClass();
            //没有返回值,有一个object类型的参数的委托;两种写法。
            ParameterizedThreadStart threadStart1 = t => tc.TestThread(t.ToString());
            //ParameterizedThreadStart threadStart2 = new ParameterizedThreadStart(tc.TestThread);
            List<Thread> threadList = new List<Thread>();
            for (int i = 0; i < 5; i++)
            {
                Thread thread = new Thread(threadStart1);
                thread.Start("test" + i);
                threadList.Add(thread);
            }

            //等待Thread执行结束1,
            while (threadList.Count(c => c.ThreadState == ThreadState.Running) > 0)
            {
                Thread.Sleep(1000);
            }

            //等待Thread执行结束2,附加都主线程,
            //主线程会等待附加的线程结束,然后再执行下面的操作,会卡界面。
            //foreach (var item in threadList)
            //{
            //    item.Join();
            //}

            Console.Read();
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-01
  • 2022-12-23
  • 2021-10-06
  • 2021-10-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-03
  • 2022-12-23
  • 2021-09-04
相关资源
相似解决方案