Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » non ui thread
non ui thread [message #528465] Tue, 20 April 2010 15:41 Go to next message
Lidder is currently offline LidderFriend
Messages: 46
Registered: July 2009
Member
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 Go to previous message
Eclipse UserFriend
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.
>
>
>
Previous Topic:Refreshing on a Mac
Next Topic:Connection from Birt to mysql with connector J
Goto Forum:
  


Current Time: Wed Apr 24 22:52:47 GMT 2024

Powered by FUDForum. Page generated in 0.08029 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top