Display.getDefault().asyncExec无法正常运行 [英] Display.getDefault().asyncExec not running correctly

查看:512
本文介绍了Display.getDefault().asyncExec无法正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用eclipse的job API将大任务作为作业运行,一旦任务完成,我将布尔变量设置为true,如果该变量为true,则在UI线程中执行WizardDialog.我当前的代码如下:

I am using eclipse's jobs API to run big task as a job, once task is completed I am setting boolean variable to true and if that variable is true I am executing WizardDialog in UI thread. My current code looks like this:

Job longRunningJob = new Job("Long running job...") {
    @Override
    protected IStatus run(IProgressMonitor monitor) {

        boolean shouldShowDialog = doLongRunningJob();

        if(shouldShowDialog) {
          Display.getDefault().asyncExec(new Runnable() {
               @Override
               public void run() {
                    //Will open wizard dialog here
                    WizardDialog wizardDialog = new WizardDialog(Display.getCurrent().getActiveShell(), new TestWizard());
                    wizardDialog.setBlockOnOpen(true);
                    wizardDialog.open();
               }
          });
        }
    }
}

longRunningJob.setUser(true);
longRunningJob.schedule();

我的问题是Display线程内的run无法以可靠的方式执行,这意味着有时它会进入run方法中,而有时却不在,我尝试将断点放入run方法中并对其进行测试,但同样发生.

My problem is run inside Display thread not executing in reliable way, means sometime it goes inside run method where as sometimes it doesn't, I tried putting breakpoint inside run method and testing it out but same happens.

我的问题是,我在做什么是正确的方式?这是预期的行为吗?那么我该如何处理这种情况,即一旦shouldShowDialog为true,如何在显示线程内执行代码?

My question is, is what I am doing is correct way? Is this expected behaviour? So how do I handle this scenario ie once shouldShowDialog is true how do I execute code inside Display thread?

编辑:我在调试时观察到的一种行为是显示对话框,但突然关闭,我认为它正在退出线程.

Edit: One behaviour I observed while debugging is dialog gets displayed but suddenly it get closes, I think it's exiting the thread.

推荐答案

对话框消失的问题最常见的原因是使用当前活动的Shell作为对话框的父级.例如.如果在创建对话框时打开了一个ProgressDialog,则另一个对话框将成为该对话框的父级.而当另一个对话框关闭时,您的对话框也将关闭.

The problem with disappearing dialogs is most commonly caused by using currently active Shell as the parent for the dialog. E.g. if there is a ProgressDialog open when you create your dialog then that other dialog will be the parent of your dialog. And when the other dialog closes, so does yours.

请使用类似以下内容的

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

这篇关于Display.getDefault().asyncExec无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆