Java SWT和无效的线程访问 [英] Java SWT and Invalid Thread Access

查看:97
本文介绍了Java SWT和无效的线程访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过这个,但它不起作用为了我的代码。这是我唯一的类:

I've seen this but it doesn't work for my code. This is my unique class:

public static void main(String[] args) {
        try {
            Main window = new Main();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

protected void createContents() {
        shell = new Shell(SWT.CLOSE | SWT.MIN | SWT.TITLE | SWT.ON_TOP);
        shell.setSize(301, 212);
        shell.setText("MyShell");
        // ...Other contents... 
        btn = new Button(shell, SWT.NONE);
        btn.setBounds(114, 151, 76, 25);
        btn.setText("BUTTON!");
        btn.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                doSomething();
            }
        });
}

方法 doSomething()是另一种方法的调用者,如下所示:

The method doSomething() is a caller for another method, like this:

private void doSomething()
{
    Thread th = new Thread() {
        public void run() {
        threadMethod();
        }
    };
    th.start();
}

当我点击我的按钮时,一个无效的线程访问从Thread-提出0,它指向 threadMethod()的第一条指令(不访问到UI小部件)。我试图用

When I click my button, an "Invalid Thread Access" raises from Thread-0, and it points to the first instruction of threadMethod() (wich doesn't access to UI widgets). I've tried to surround my button listener with

Display.getDefault().asyncExec(new Runnable() {
    public void run() {
        // ...
    }
});

但它也不起作用。我需要 doSomething()方法,因为它在创建线程之前检查一些代码。
这是threadMethod()

but it doesn't work either. I need the doSomething() method because it checks some code before creating the thread. This is threadMethod()

private void threadMethod() 
    {
        String[] listItems = list.getItems();
        String fileName;
        Path source, target;
        File folder = new File(dir + File.separator);
        if (!folder.exists()) {
            folder.mkdir();
        }
        try
        {
            for(int i = 0; i < list.getItemCount(); i++) 
            {

                // do something with non UI widgets
            }

            list.removeAll();

        }
        catch(IOException | InterruptedException e)
        {
            //print error

        }
    }

为什么我的线程访问无效?谢谢!

Why I've got Invalid thread access? Thank you!

推荐答案

List是一个SWT小部件,如果你在UI Thread之外调用它的getItems()方法(在这种情况下你的主线程),你得到一个ERROR_THREAD_INVALID_ACCESS SWTException。这在List API :ERROR_THREAD_INVALID_ACCESS - 如果没有从创建接收器的线程调用

List is an SWT widget and if you call the getItems() method on it outside of the UI Thread (in this case your main thread), you get an ERROR_THREAD_INVALID_ACCESS SWTException. This is defined in the List API: ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

创建接收器的线程是创建显示的线程。如果Display不存在,则第一次调用Display.getDefault()会创建一个。因此,调用open()方法的主线程是UI线程。如果你包装threadMethod()的内容,你的代码将会起作用:

The Thread that created the receiver, is the the thread that created the Display. If a Display does not exist, the first call to Display.getDefault() creates one. Therefore your main thread, which calls the open() method, is the UI thread. Your code will work if you wrap the contents of the threadMethod():

private void threadMethod() {
    Display.getDefault().asyncExec(new Runnable() {
        public void run() {
            // threadMethod contents
        }
    });
} 

然后将在UI线程中执行。

It will then be executed in the UI thread.

这篇关于Java SWT和无效的线程访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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