Using Display.syncExec to access UI thread from an IServiceHandler instance leads to a deadlock [message #639138] |
Mon, 15 November 2010 11:48 |
Michal Tkacz 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 #639572 is a reply to message #639472] |
Tue, 16 November 2010 22:50 |
|
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/
|
|
|
Powered by
FUDForum. Page generated in 0.02751 seconds