1

我正在为 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 线程的调用)..

如果渲染引擎不异步完成他的工作会容易得多。

4

1 回答 1

0

我不认为这是最好的答案,但希望它是您正在寻找的。这就是阻止线程继续的方式。

    // Your UI thread should already have a Dispatcher object. If you do this elsewhere, then you will need your class to inherit DispatcherObject.
    private DispatcherFrame ThisFrame;

    public void Main()
    {
        // Pausing the Thread
        Pause();
    }

    public void Pause()
    {
        ThisFrame = new DispatcherFrame(true);
        Dispatcher.PushFrame(ThisFrame);
    }

    public void UnPause()
    {
        if (ThisFrame != null && ThisFrame.Continue)
        {
             ThisFrame.Continue = false;
             ThisFrame = null;
        }
    }

如果您想在中间阻塞的同时仍然接收并执行该线程上的操作,您可以执行类似的操作。这感觉,嗯……有点 hacky,所以不要只是复制和粘贴而不确保我没有犯一些重大错误。我还没喝咖啡。

// Used while a work item is processing. If you have something that you want to wait on this process. Or you could use event handlers or something.
private DispatcherFrame CompleteFrame;
// Controls blocking of the thread.
private DispatcherFrame TaskFrame;

// Set to true to stop the task manager.
private bool Close;

// A collection of tasks you want to queue up on this specific thread. 
private List<jTask> TaskCollection;

public void QueueTask(jTask newTask)
{
    //Task Queued.

    lock (TaskCollection) { TaskCollection.Add(newTask); }
    if (TaskFrame != null) { TaskFrame.Continue = false; }
}

// Call this method when you want to start the task manager and let it wait for a task.
private void FireTaskManager()
{
    do
    {
        if (TaskCollection != null)
        {
            if (TaskCollection.Count > 0 && TaskCollection[0] != null) 
            { 
                ProcessWorkItem(TaskCollection[0]);
                lock (TaskCollection) { TaskCollection.RemoveAt(0); }
            }
            else { WaitForTask(); }
        }
    }
    while (!Close);
}

// Call if you are waiting for something to complete.
private void WaitForTask()
{
    if (CompleteFrame != null) { CompleteFrame.Continue = false; }

    // Waiting For Task.

    TaskFrame = new DispatcherFrame(true);
    Dispatcher.PushFrame(TaskFrame);
    TaskFrame = null;
}

/// <summary>
/// Pumping block will release when all queued tasks are complete. 
/// </summary>
private void WaitForComplete()
{
    if (TaskCollection.Count > 0)
    {
        CompleteFrame = new DispatcherFrame(true);
        Dispatcher.PushFrame(CompleteFrame);
        CompleteFrame = null;
    }
}

private void ProcessWorkItem(jTask taskItem)
{
    if (taskItem != null) { object obj = taskItem.Go(); }
}
于 2013-08-06T14:55:20.327 回答