我需要在工作线程完成时通知主线程。当我接受一个委托并在另一个线程完成时执行它,它就会在该线程上执行,这不是我想要的。由于我的一些限制('更新'在Unity Editor中没有调用每一帧),我都无法检查它是否已完成。我有其他选择吗?
答案 0 :(得分:2)
//This is a helper coroutine
IEnumerable RunOffMainThread(Action toRun, Action callback) {
bool done = false;
new Thread(()=>{
toRun();
done = true;
}).Start();
while (!done)
yield return null;
callback();
}
//This is the method you call to start it
void DoSomethingOffMainThread() {
StartCoroutine(RunOffMainThread(ToRun, OnFinished));
}
//This is the method that does the work
void ToRun() {
//Do something slow here
}
//This is the method that's called when finished
void OnFinished() {
//off main thread code finished, back on main thread now
}