Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » newbie: communicating with UI thread
newbie: communicating with UI thread [message #451874] Thu, 10 March 2005 11:38 Go to next message
Eclipse UserFriend
Originally posted by: martin.j.nilsson.sverige.nu

Hi,

This seems like a trivial problem to me, but addmittedly I'm rather new to
Threads which might explain my ignorance.

I have this class which takes a couple of arguments and performs, among
other things, some rather large filecopying. It writes its output to
standard out. I've created a SWT GUI that basically will be used to show
the text strings that used to be written to standard out.

In the GUI I have a public method called addMessage(String message) which
is the entry-point to which to send the text-strings, it adds the text to
the textarea.

Now, from the main class I've understod that I'll have to create a new
thread to execute all the things (file copying etc) that used to write its
output to stdout. I've also created a small method in the main class to
act as a communicator with the GUI:

// Note this is pseudo-code, might be incorrect.
public void addMessage(final String message);
display.syncExec (new Runnable () {
public void run () {
splash.addMessage(message);
}
});
}

My plan is to switch all System.out.prinln to calling this method instead,
but whenever I use it I get a Invalid Thread access.

There must be something very simple that I'm overlooking, I've tried
reading the FAQ, but it basically says that if I wrap code
(display.syncExec) in the above way it should always work.

Any help is greatly appreciated!

/martin
Re: newbie: communicating with UI thread [message #451884 is a reply to message #451874] Thu, 10 March 2005 15:57 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
If you wrap your call to the SWT widgets in display.syncExec, you should not
get an invalid thread exception. Either, you are creating another thread
within splash.addMessage which is causing the problem or you have created
multiple Display objects in your application and you are getting them
crossed.

Be careful about using Display.getDefault(). If this is called before any
Display object is created, it will create a new display. If you then create
a Display using the constructor, you will end up with two.

"Martin J Nilsson" <martin.j.nilsson@sverige.nu> wrote in message
news:d0pbj1$fkm$1@www.eclipse.org...
> Hi,
>
> This seems like a trivial problem to me, but addmittedly I'm rather new to
> Threads which might explain my ignorance.
>
> I have this class which takes a couple of arguments and performs, among
> other things, some rather large filecopying. It writes its output to
> standard out. I've created a SWT GUI that basically will be used to show
> the text strings that used to be written to standard out.
>
> In the GUI I have a public method called addMessage(String message) which
> is the entry-point to which to send the text-strings, it adds the text to
> the textarea.
>
> Now, from the main class I've understod that I'll have to create a new
> thread to execute all the things (file copying etc) that used to write its
> output to stdout. I've also created a small method in the main class to
> act as a communicator with the GUI:
>
> // Note this is pseudo-code, might be incorrect.
> public void addMessage(final String message);
> display.syncExec (new Runnable () {
> public void run () {
> splash.addMessage(message); }
> });
> }
>
> My plan is to switch all System.out.prinln to calling this method instead,
> but whenever I use it I get a Invalid Thread access.
> There must be something very simple that I'm overlooking, I've tried
> reading the FAQ, but it basically says that if I wrap code
> (display.syncExec) in the above way it should always work.
>
> Any help is greatly appreciated!
>
> /martin
>
Re: newbie: communicating with UI thread [message #451927 is a reply to message #451884] Fri, 11 March 2005 09:38 Go to previous message
Eclipse UserFriend
Originally posted by: martin.j.nilsson.sverige.nu

Veronika Irvine wrote:

> If you wrap your call to the SWT widgets in display.syncExec, you should not
> get an invalid thread exception. Either, you are creating another thread
> within splash.addMessage which is causing the problem or you have created
> multiple Display objects in your application and you are getting them
> crossed.

> Be careful about using Display.getDefault(). If this is called before any
> Display object is created, it will create a new display. If you then create
> a Display using the constructor, you will end up with two.

Veronika, thank you very much! I was getting Display objects crossed, but
when I corrected that I got another error which I didn't understand. While
I was writing you this answer (which was a whole lot longer including a
lot of code-listings) I understod what I was doing wrong. Now everything
works fine.

Thanks again!

regards,
martin
Previous Topic:Survey on Understanding Code
Next Topic:SWT & OpenGL
Goto Forum:
  


Current Time: Mon Sep 23 02:37:12 GMT 2024

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

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

Back to the top