Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Multithreading and Widgets
|
Re: Multithreading and Widgets [message #433670 is a reply to message #433553] |
Sun, 11 April 2004 20:47 |
Olivier Chalouhi Messages: 22 Registered: July 2009 |
Junior Member |
|
|
1. keep a reference to the display (display) and to your text area
(text) then all you need to do is :
private void updateText(final String newText) {
//Always test for null or disposal
if(display == null || display.isDisposed()) return;
display.aSyncExec(new Runnable() {
public void run() {
//Test AGAIN for disposal on any used SWT elements here.
if(text == null || text.isDisposed()) return;
text.setText(newText);
}
});
}
I'm not sure it'll conpile, just typed it here ...
Basically you 'post' runnable to the the GUI Thread (the one which is
doing the loop :
if(!display.readAndDispach()) display.sleep;
)
Good luck, SWT rocks.
Olivier Chalouhi.
On 2004-04-10 18:51:09 +0200, goldwing@nospam.socal.rr.com (Chris) said:
> I wrote a chat program in Swing and I have worked for the last couple days
> to convert it to SWT. Unfortunately, I have discovered a big problem with
> the design of SWT that prevents me from updating the output text area from
> another thread when the server sends text back.
>
> After reading the documentation, I thought that changing this:
> new Thread(this).start();
> in my main class to:
> Display.getCurrent().syncExec(this);
> would fix the problem, but all it seems to do is put the socket listener
> and the GUI controls in the same thread! Once I connect, the whole
> program hangs until I kill the connection from the server.
>
> The only solution I could think of is to declare a String variable in my
> GUI class, and have the connection thread write all of its updates to
> it. Then my GUI thread would have to keep strobing the variable to
> figure out
> when it changes.
>
> There has to be a better solution, right?
>
> BTW, the connection listener (run() method) and the GUI controls are in
> the same class.
|
|
|
Re: Multithreading and Widgets [message #433678 is a reply to message #433670] |
Mon, 12 April 2004 03:17 |
Eclipse User |
|
|
|
Originally posted by: news.vadalus.com
Also -- you should be doing something similar in SWING as well. SWING is
not thread safe, and unlike SWT will not force you use the UI thread
when updating the UI.
This is one of the reasons people get unexpected behaviour in their
swing apps.
Aelitis wrote:
> 1. keep a reference to the display (display) and to your text area
> (text) then all you need to do is :
>
> private void updateText(final String newText) {
> //Always test for null or disposal
> if(display == null || display.isDisposed()) return;
>
> display.aSyncExec(new Runnable() {
> public void run() {
> //Test AGAIN for disposal on any used SWT elements here.
> if(text == null || text.isDisposed()) return;
> text.setText(newText);
> }
> });
>
> }
>
>
> I'm not sure it'll conpile, just typed it here ...
>
> Basically you 'post' runnable to the the GUI Thread (the one which is
> doing the loop :
> if(!display.readAndDispach()) display.sleep;
> )
>
> Good luck, SWT rocks.
>
> Olivier Chalouhi.
>
> On 2004-04-10 18:51:09 +0200, goldwing@nospam.socal.rr.com (Chris) said:
>
>> I wrote a chat program in Swing and I have worked for the last couple
>> days
>> to convert it to SWT. Unfortunately, I have discovered a big problem
>> with
>> the design of SWT that prevents me from updating the output text area
>> from
>> another thread when the server sends text back.
>>
>> After reading the documentation, I thought that changing this:
>> new Thread(this).start();
>> in my main class to:
>> Display.getCurrent().syncExec(this);
>> would fix the problem, but all it seems to do is put the socket listener
>> and the GUI controls in the same thread! Once I connect, the whole
>> program hangs until I kill the connection from the server.
>>
>> The only solution I could think of is to declare a String variable in my
>> GUI class, and have the connection thread write all of its updates to
>> it. Then my GUI thread would have to keep strobing the variable to
>> figure out
>> when it changes.
>>
>> There has to be a better solution, right?
>>
>> BTW, the connection listener (run() method) and the GUI controls are in
>> the same class.
>
>
>
|
|
|
Re: Multithreading and Widgets [message #439754 is a reply to message #433678] |
Thu, 15 July 2004 19:00 |
Oleg Messages: 7 Registered: July 2009 |
Junior Member |
|
|
I need to do the same thing, but from inside of the plugin. How can I
get a reference to the display variable from inside the plugin, say
ViewPart ?
Oleg
vadalus wrote:
> Also -- you should be doing something similar in SWING as well. SWING is
> not thread safe, and unlike SWT will not force you use the UI thread
> when updating the UI.
>
> This is one of the reasons people get unexpected behaviour in their
> swing apps.
>
>
> Aelitis wrote:
>
>> 1. keep a reference to the display (display) and to your text area
>> (text) then all you need to do is :
>>
>> private void updateText(final String newText) {
>> //Always test for null or disposal
>> if(display == null || display.isDisposed()) return;
>>
>> display.aSyncExec(new Runnable() {
>> public void run() {
>> //Test AGAIN for disposal on any used SWT elements here.
>> if(text == null || text.isDisposed()) return;
>> text.setText(newText);
>> }
>> });
>>
>> }
>>
>>
>> I'm not sure it'll conpile, just typed it here ...
>>
>> Basically you 'post' runnable to the the GUI Thread (the one which is
>> doing the loop :
>> if(!display.readAndDispach()) display.sleep;
>> )
>>
>> Good luck, SWT rocks.
>>
>> Olivier Chalouhi.
>>
>> On 2004-04-10 18:51:09 +0200, goldwing@nospam.socal.rr.com (Chris) said:
>>
>>> I wrote a chat program in Swing and I have worked for the last couple
>>> days
>>> to convert it to SWT. Unfortunately, I have discovered a big problem
>>> with
>>> the design of SWT that prevents me from updating the output text area
>>> from
>>> another thread when the server sends text back.
>>>
>>> After reading the documentation, I thought that changing this:
>>> new Thread(this).start();
>>> in my main class to:
>>> Display.getCurrent().syncExec(this);
>>> would fix the problem, but all it seems to do is put the socket listener
>>> and the GUI controls in the same thread! Once I connect, the whole
>>> program hangs until I kill the connection from the server.
>>>
>>> The only solution I could think of is to declare a String variable in my
>>> GUI class, and have the connection thread write all of its updates to
>>> it. Then my GUI thread would have to keep strobing the variable to
>>> figure out
>>> when it changes.
>>>
>>> There has to be a better solution, right?
>>>
>>> BTW, the connection listener (run() method) and the GUI controls are in
>>> the same class.
>>
>>
>>
>>
|
|
|
Re: Multithreading and Widgets [message #439755 is a reply to message #439754] |
Thu, 15 July 2004 19:01 |
Oleg Messages: 7 Registered: July 2009 |
Junior Member |
|
|
OOps, diregard this -- I am an idiot -- parent.getDisplay() does this.
Oleg
Oleg wrote:
> I need to do the same thing, but from inside of the plugin. How can I
> get a reference to the display variable from inside the plugin, say
> ViewPart ?
>
> Oleg
>
>
> vadalus wrote:
>
>> Also -- you should be doing something similar in SWING as well. SWING
>> is not thread safe, and unlike SWT will not force you use the UI
>> thread when updating the UI.
>>
>> This is one of the reasons people get unexpected behaviour in their
>> swing apps.
>>
>>
>> Aelitis wrote:
>>
>>> 1. keep a reference to the display (display) and to your text area
>>> (text) then all you need to do is :
>>>
>>> private void updateText(final String newText) {
>>> //Always test for null or disposal
>>> if(display == null || display.isDisposed()) return;
>>>
>>> display.aSyncExec(new Runnable() {
>>> public void run() {
>>> //Test AGAIN for disposal on any used SWT elements here.
>>> if(text == null || text.isDisposed()) return;
>>> text.setText(newText);
>>> }
>>> });
>>>
>>> }
>>>
>>>
>>> I'm not sure it'll conpile, just typed it here ...
>>>
>>> Basically you 'post' runnable to the the GUI Thread (the one which is
>>> doing the loop :
>>> if(!display.readAndDispach()) display.sleep;
>>> )
>>>
>>> Good luck, SWT rocks.
>>>
>>> Olivier Chalouhi.
>>>
>>> On 2004-04-10 18:51:09 +0200, goldwing@nospam.socal.rr.com (Chris) said:
>>>
>>>> I wrote a chat program in Swing and I have worked for the last
>>>> couple days
>>>> to convert it to SWT. Unfortunately, I have discovered a big
>>>> problem with
>>>> the design of SWT that prevents me from updating the output text
>>>> area from
>>>> another thread when the server sends text back.
>>>>
>>>> After reading the documentation, I thought that changing this:
>>>> new Thread(this).start();
>>>> in my main class to:
>>>> Display.getCurrent().syncExec(this);
>>>> would fix the problem, but all it seems to do is put the socket
>>>> listener
>>>> and the GUI controls in the same thread! Once I connect, the whole
>>>> program hangs until I kill the connection from the server.
>>>>
>>>> The only solution I could think of is to declare a String variable
>>>> in my
>>>> GUI class, and have the connection thread write all of its updates
>>>> to it. Then my GUI thread would have to keep strobing the variable
>>>> to figure out
>>>> when it changes.
>>>>
>>>> There has to be a better solution, right?
>>>>
>>>> BTW, the connection listener (run() method) and the GUI controls are in
>>>> the same class.
>>>
>>>
>>>
>>>
>>>
|
|
|
Goto Forum:
Current Time: Sat Apr 27 04:55:47 GMT 2024
Powered by FUDForum. Page generated in 0.02777 seconds
|