等待线程实际在c#中启动

Guy*_*y L 9 .net c# multithreading

我需要启动一个线程,但在线程实际运行继续.现在我的代码看起来像:

        splashthread.IsBackground = false;
        splashthread.Start();
        Thread.Sleep(100); // Wait for the thread to start
Run Code Online (Sandbox Code Playgroud)

我不喜欢这些伏都教睡眠(至少可以说),所以我正在寻找更好的方式来做上述事情.

有任何想法吗?

Ili*_*ian 20

像这样的东西:

var splashStart = new ManualResetEvent(false);

var splashthread = new Thread(
  () =>
  {
     splashStart.Set();
     // Your thread code here...
  });

splashthread.Start();
splashStart.WaitOne();
Run Code Online (Sandbox Code Playgroud)

不要忘记Dipose splashStart或者如果它在你的代码中合适使用一个using块.

编辑:未在IDE中确认原始代码.根据下面的评论,将等待更改为WaitOne().