阻止UI线程,但继续处理传入的呼叫

时间:2013-08-06 13:38:03

标签: c# c#-4.0

我正在为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);
    }
}

然而,这不会奏效。 renderplugin似乎做了一些异步工作,但是,当执行这个异步工作时,它正在调用主线程来检索信息。

我找到了一个解决方法:在调用渲染引擎进行渲染之后,调用MessageBox。这将阻止代码继续,但仍然可以处理异步调用。我知道,这是奇怪的行为。甚至更奇怪的是,当renderengine调用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);
    }
}

除了messagebox部分外,这很棒。我不想显示消息框,我只想暂停我的代码而不阻塞整个线程(因为仍然接受从renderengine到ui线程的调用)..

如果renderengine没有让他的工作异步,那将会容易得多。

1 个答案:

答案 0 :(得分: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(); }
}