Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Using Display.syncExec to access UI thread from an IServiceHandler instance leads to a deadlock
Using Display.syncExec to access UI thread from an IServiceHandler instance leads to a deadlock [message #639138] Mon, 15 November 2010 11:48 Go to next message
Michal Tkacz is currently offline Michal TkaczFriend
Messages: 105
Registered: July 2009
Senior Member
Please take a look at the following snippet. What I'm trying to do here
is access fileNameText widget from an IServiceHandler instance which is
supposed to handle a download process (it is triggered by a browser
link). In this case I cannot use asyncExec. But if I use syncExec the
IServiceHandler thread hangs. Is it an error, or a deadlock I should
expect in this case? Thanks.


public class EntryPoint implements IEntryPoint {

@Override
public int createUI() {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(2, false));
new Label(shell, SWT.NONE).setText("File name:");
final Text fileNameText = new Text(shell, SWT.NONE);
fileNameText.setText("file.txt");
fileNameText.setLayoutData(
new GridData(SWT.FILL, SWT.NONE, true, false));
Browser browser = new Browser(shell, SWT.NONE);
browser.setLayoutData(
new GridData(SWT.FILL, SWT.FILL, false, true, 2, 1));
HttpServletRequest request = RWT.getRequest();
browser.setText("<a href=\"" + request.getContextPath()
+ request.getServletPath() + "?" + IServiceHandler.REQUEST_PARAM
+ "=download\">click me<a>");

IServiceManager manager = RWT.getServiceManager();
IServiceHandler handler = new IServiceHandler() {

@Override
public void service() throws IOException, ServletException {
display.syncExec(new Runnable() {

@Override
public void run() {
String fileName = fileNameText.getText();
byte[] content = "Hello world!".getBytes();
HttpServletResponse response = RWT.getResponse();
response.setContentType("application/octet-stream");
response.setContentLength(content.length);
String contentDisposition = "attachment; filename=\""
+ fileName + "\"";
response.setHeader("Content-Disposition",
contentDisposition);

try {
response.getOutputStream().write(content);
} catch(IOException e1) {
e1.printStackTrace();
}
}
});
}
};

manager.registerServiceHandler("download", handler);

shell.pack();
shell.open();

while(!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}

return 0;
}
}
Re: Using Display.syncExec to access UI thread from an IServiceHandler instance leads to a deadlock [message #639408 is a reply to message #639138] Tue, 16 November 2010 12:47 Go to previous messageGo to next message
Ralf Sternberg is currently offline Ralf SternbergFriend
Messages: 1313
Registered: July 2009
Senior Member

Hi Michal,

Display#syncExec should generally used with care, and calling syncExec
from a separate request seems a bad idea to me. This will block the
calling thread, i.e. the service handler request until the runnable is
processed in the lifecycle of the normal request!

I'd recommend to decouple both requests by storing the fileName in a
synchronized (!) field on modification or focus out, and then access this
field in the service handler instead of accessing the widget itself.

Hope this helps,
Ralf

--
Ralf Sternberg

Need professional support for RAP and RCP?
http://www.eclipsesource.com/support/

Twitter: http://twitter.com/eclipsesource
Blog: http://www.eclipsesource.com/blogs/
Re: Using Display.syncExec to access UI thread from an IServiceHandler instance leads to a deadlock [message #639472 is a reply to message #639408] Tue, 16 November 2010 16:29 Go to previous messageGo to next message
Michal Tkacz is currently offline Michal TkaczFriend
Messages: 105
Registered: July 2009
Senior Member
On 11/16/2010 01:47 PM, Ralf Sternberg wrote:
> Hi Michal,
>
> Display#syncExec should generally used with care, and calling syncExec
> from a separate request seems a bad idea to me. This will block the
> calling thread, i.e. the service handler request until the runnable is
> processed in the lifecycle of the normal request!

Do I understand correctly that it will be blocked until another action
in the browser (like clicking on a button, entering text etc.) makes it
send a standard UI request to the server? That would be unfortunate
because what the user does after clicking on a download link is exactly
opposite - he just sits there waiting for a download to begin.
Is there a technical reason why (a)syncExec requests cannot be handled
instantly if UI thread is not busy handling standard UI requests?

> I'd recommend to decouple both requests by storing the fileName in a
> synchronized (!) field on modification or focus out, and then access this
> field in the service handler instead of accessing the widget itself.

That's what I did eventually, thank you.

Michal

>
> Hope this helps,
> Ralf
>
Re: Using Display.syncExec to access UI thread from an IServiceHandler instance leads to a deadlock [message #639572 is a reply to message #639472] Tue, 16 November 2010 22:50 Go to previous message
Ralf Sternberg is currently offline Ralf SternbergFriend
Messages: 1313
Registered: July 2009
Senior Member

On Tue, 16 Nov 2010 17:29:41 +0100, Michał Tkacz wrote:

> Do I understand correctly that it will be blocked until another action
> in the browser (like clicking on a button, entering text etc.) makes it
> send a standard UI request to the server? That would be unfortunate
> because what the user does after clicking on a download link is exactly
> opposite - he just sits there waiting for a download to begin. Is there
> a technical reason why (a)syncExec requests cannot be handled instantly
> if UI thread is not busy handling standard UI requests?

syncExec calls will be blocked until the runnable has been processed. The
runnable is being processed in the UIThread. The UIThread can only run
during the processing of a request. Otherwise the client- and server-side
widgets would run out of sync.

Ralf

--
Ralf Sternberg

Need professional support for RAP and RCP?
http://www.eclipsesource.com/support/

Twitter: http://twitter.com/eclipsesource
Blog: http://www.eclipsesource.com/blogs/
Previous Topic:SWT widget #getLocation() returns a wrong value
Next Topic:How to display tooltips for its content in the tableviewer
Goto Forum:
  


Current Time: Mon Sep 23 20:43:12 GMT 2024

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

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

Back to the top