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().
归档时间: |
|
查看次数: |
5469 次 |
最近记录: |