在线程之间切换

gip*_*ipo 0 c# multithreading join

我有两个主题:

static Thread thread1 = new Thread(new ThreadStart(Team1Shots));
static Thread thread2 = new Thread(new ThreadStart(Team2Shots));
Run Code Online (Sandbox Code Playgroud)

我希望thread1做一些工作(但不完整),然后thread2做一些工作(但不完整),然后回去完成thread1,然后回去完成thread2.

到目前为止我有这个:

static void Main(string[] args)
{
    thread1.Start();
}

private static void Team1Shots()
{
   //Do Work in here...then

   thread2.Start();
   thread2.Join(); //Go to thread2

   //When join in thread2 to here
   //Do the rest of the work

   //Get thread2 to finish
}

private static void Team2Shots()
{
   //Do Work in here...
   thread1.Join(); //Go back to thread1

   //When thread1 finishes
   //Do the rest of the work

   // Finished All work
}
Run Code Online (Sandbox Code Playgroud)

这不起作用,不知道从哪里开始.

Ser*_*rvy 6

既然你实际上并不想要并行完成任何工作,而只是让其中一个工作一次完成,你应该没有两个线程.有一个线程可以完成所有应该首先完成的事情,然后是所有应该完成的事情,然后是接下来应该完成的所有事情,依此类推.通过创建启动/停止彼此进度的多个线程,您不必要地创建工作.