我需要启动一个线程,但是在实际运行之后,仅继续。现在我的代码如下: splashthread.IsBackground = false;
splashthread.Start();
Thread.Sleep(100); // Wait for the thread to start
我不喜欢这些伏都教的 sleep (至少可以这样说),因此我正在寻找一种更漂亮的方法来完成上述任务。
有任何想法吗?
最佳答案
像这样的东西:
var splashStart = new ManualResetEvent(false);
var splashthread = new Thread(
() =>
{
splashStart.Set();
// Your thread code here...
});
splashthread.Start();
splashStart.WaitOne();
不要忘记放掉
splashStart
,或者如果适合在您的代码中使用using
块。编辑:未在IDE中确认原始代码。根据下面的注释将Wait更改为WaitOne()。