non ui thread [message #528465] |
Tue, 20 April 2010 15:41 |
|
Hello,
I am using a non UI thread to do some work in the background as follows:
Thread.run method
{
while(true)
{
// do some checks and redraw the UI if need be.
PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
public void run() {
// do some work on UI items -> MyItem.redraw()
}
});
}
}
I want the thead to run for the lifetime of my application. The problem is that if I close my application, I get a null pointer exception in the thread. I tried putting in null checks for workbench, display and everything inside the run method. But my application still crashes. What is a good paradigm here to make sure that the application is still running before invoking the run syncExec from the non UI thread. Also once it is invoked and the application closes, will I not get the null pointer exception?
Thanks.
|
|
|
Re: non ui thread [message #528474 is a reply to message #528465] |
Tue, 20 April 2010 16:12 |
Eclipse User |
|
|
|
The best way to work async in Eclipse is to use the job API. See
http://www.eclipse.org/articles/Article-Concurrency/jobs-api .html
To avoid the exceptions use the following code:
Label l;
Job j = new Job(){
public IStatus run(IProgressMonitor monitor){
while(!monitor.isCanceled()){
l.getDisplay().syncExec(new Runnable(){
public void run(){
if(l != null && l.isDisposed()){
l.setText("some updates")
}
}
});
}
}
};
somewhere else:
l.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
j.cancel();
}
});
Hope it helps
Andreas
Lidder wrote:
> Hello,
>
> I am using a non UI thread to do some work in the background as follows:
>
> Thread.run method
> {
>
> while(true)
> {
>
> // do some checks and redraw the UI if need be.
> PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
> public void run() {
> // do some work on UI items -> MyItem.redraw()
> }
> });
> }
> }
> I want the thead to run for the lifetime of my application. The problem
> is that if I close my application, I get a null pointer exception in the
> thread. I tried putting in null checks for workbench, display and
> everything inside the run method. But my application still crashes. What
> is a good paradigm here to make sure that the application is still
> running before invoking the run syncExec from the non UI thread. Also
> once it is invoked and the application closes, will I not get the null
> pointer exception?
>
> Thanks.
>
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.08027 seconds