SWT does not allow non-gui threads to update gui thread components.
So you have to make use of the "asyncExec(Runnable)" and "syncExec(Runnable)" calls in "Display".
The *Exec() call will allow the GUI thread to run whatever runnable object passed as a parameter and run the object in a separate gui thread.
For more detailed explanation take a look at the API. Here's a link http://download.eclipse.org/eclipse/downloads/documentation/2.0/html/plugins/org.eclipse.platform.doc.isv/reference/api/
I've included an example for clarity.
I've created two classes: SWTGuiExample, and GUIUpdater.
SWTGuiExample contains the GUI widgets and GUIUpdater updates the progress bar using SWTGuiExample's UpdateStatusBar method;
the interesting part for this discussion is in "public void UpdateProgressBar( final int selection_value )".
So you have to make use of the "asyncExec(Runnable)" and "syncExec(Runnable)" calls in "Display".
The *Exec() call will allow the GUI thread to run whatever runnable object passed as a parameter and run the object in a separate gui thread.
For more detailed explanation take a look at the API. Here's a link http://download.eclipse.org/eclipse/downloads/documentation/2.0/html/plugins/org.eclipse.platform.doc.isv/reference/api/
I've included an example for clarity.
I've created two classes: SWTGuiExample, and GUIUpdater.
SWTGuiExample contains the GUI widgets and GUIUpdater updates the progress bar using SWTGuiExample's UpdateStatusBar method;
the interesting part for this discussion is in "public void UpdateProgressBar( final int selection_value )".
// ----------------------------------------------------------------------------------------
// SWTGuiExample.java
// ----------------------------------------------------------------------------------------
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import java.lang.Runnable;
public class SWTGuiExample
{
private Shell main_shell;
private Display main_display;
private ProgressBar pBar_status;
private int pBar_selection;
public static final int PROGRESS_BY = 2;
public SWTGuiExample()
{
pBar_selection = 0;
}
public void BuildGUI()
{
// initialize display and shell
main_display = new Display();
main_shell = new Shell( main_display );
main_shell.setSize(500, 250);
// add progress bar
pBar_status = new ProgressBar( main_shell, SWT.SMOOTH );
pBar_status.setSize(400,20);
pBar_status.setMaximum( 100 );
pBar_status.setMinimum( 0 );
// open shell
main_shell.open();
}
public void DestroyGUI()
{
main_display.dispose();
}
public void UpdateProgressBar( final int selection_value )
{
if( pBar_status.isDisposed() ) return;
main_display.asyncExec
(
new Runnable()
{
public void run() { pBar_status.setSelection( selection_value ); }
}
);
}
public Shell GetShell() { return main_shell; }
public Display GetDisplay() { return main_display; }
public static void main( String[] args )
{
SWTGuiExample gui = new SWTGuiExample();
gui.BuildGUI();
new Thread( new GUIUpdater(gui) ).start();
while( !gui.GetShell().isDisposed() )
{
if( !gui.GetDisplay().readAndDispatch() )
{
gui.GetDisplay().sleep();
}
}
gui.DestroyGUI();
}
}
// ----------------------------------------------------------------------------------------
// GUIUpdater.java
// ----------------------------------------------------------------------------------------
import java.lang.Thread;
public class GUIUpdater implements Runnable
{
private SWTGuiExample m_gui;
public GUIUpdater( SWTGuiExample gui )
{
m_gui = gui;
}
public void run()
{
int current_seleciton_value = 0;
while( current_seleciton_value <= 100 )
{
m_gui.UpdateProgressBar( current_seleciton_value );
current_seleciton_value += SWTGuiExample.PROGRESS_BY;
try
{
Thread.sleep(600); //0.6 sec
}
catch( Exception e )
{}
}
}
}
本文介绍了一种在Eclipse SWT中非GUI线程更新GUI组件的方法。通过使用`asyncExec`和`syncExec`方法,可以确保在主线程中安全地更新进度条等组件。示例代码展示了如何创建一个进度条并从另一个线程更新其状态。

1497

被折叠的 条评论
为什么被折叠?



