我正在为 3D 建模应用程序开发一个插件。对于这个应用程序,还有一个我想自动化的第三方插件(渲染引擎)。
我所做的是创建一个 Camera 列表List<Camera> cameraViews
,遍历所有这些并告诉渲染引擎开始渲染
foreach ( Camera camera in cameraViews )
{
// tell the modellingApplication to apply camera
modellingApplication.ApplyCameraToView(camera);
// tell the render engine to render the image
string path = "somePathWhereIWantToSaveTheImage"
renderEngine.renderCurrentScene(path)
// .renderCurrentScene() seems to be async, because my code, which is on the UI thread
// continues... so:
// make sure that the image is saved before continuing to the next image
while ( !File.Exists(path) )
{
Thread.Sleep(500);
}
}
但是,这行不通。渲染插件似乎做了一些异步工作,但是在做这个异步工作时,它正在调用主线程来检索信息。
我找到了一个解决方法:在调用渲染引擎进行渲染之后,调用 MessageBox。这将阻止代码继续,但仍在处理异步调用。我知道,这是一种奇怪的行为。更奇怪的是,当渲染引擎完成调用 UI 线程以获取信息并继续他自己的进程时,我的 MessageBox 会自动关闭。让我的代码继续执行 while 循环以检查图像是否保存在磁盘上。
foreach ( Camera camera in cameraViews )
{
// tell the modellingApplication to apply camera
modellingApplication.ApplyCameraToView(camera);
// tell the render engine to render the image
string path = "somePathWhereIWantToSaveTheImage"
renderEngine.renderCurrentScene(path)
// .renderCurrentScene() seems to be async, because my code, which is on the UI thread
// continues... so:
// show the messagebox, as this will block the code but not the renderengine.. (?)
MessageBox.Show("Currently processed: " + path);
// hmm, messagebox gets automatically closed, that's great, but weird...
// make sure that the image is saved before continuing to the next image
while ( !File.Exists(path) )
{
Thread.Sleep(500);
}
}
这太棒了,除了消息框部分。我不想显示消息框,我只想暂停我的代码而不阻塞整个线程(因为仍然接受从渲染引擎到 ui 线程的调用)..
如果渲染引擎不异步完成他的工作会容易得多。