Many browser widgets in many threads [message #507952] |
Fri, 15 January 2010 04:54  |
Eclipse User |
|
|
|
Hi all, I'm facing a problem during the development of a test tool that implements the simulation of many different page requests from many browser instances.
I'm on a WinXP Sp2 box w/ JDK 1.6.0_16, Galileo Eclipse, Eclipse SDK 3.5.1 and SWT 3.555.
With the two following classes:
Test.java
import java.util.ArrayList;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
public class Test {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
Display display = new Display();
ArrayList<BrowserThread> brThreads = new ArrayList<BrowserThread>();
String[] sites = {"http://www.ansa.it", "http://www.apple.com"};
try {
for(String value : sites) {
BrowserThread brThr = new BrowserThread(display, value);
brThreads.add(brThr);
brThr.start();
}
for (BrowserThread brThr: brThreads) {
if (brThr.isAlive()) {
brThr.join();
}
}
} catch (SWTError e) {
System.out.println("Could not instantiate Browser: " + e.getMessage());
//shell.dispose();
return;
}
//display.dispose();
}
}
and BrowserThread.java
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* @author 11612764
*
*/
public class BrowserThread extends Thread {
private Display display;
private Shell shell;
private String url;
BrowserThread(Display display, String url) {
this.display = display;
this.url = url;
this.shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText(url);
}
public void run() {
Browser browser;
try {
browser = new Browser(this.shell, SWT.NONE);
shell.open();
browser.setUrl(url);
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) Thread.sleep(5000);
}
shell.dispose();
} catch (SWTError e) {
System.out.println("Could not instantiate Browser: " + e.getMessage());
shell.dispose();
return;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I obtained the following error:
Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.checkWidget(Unknown Source)
at org.eclipse.swt.widgets.Widget.checkParent(Unknown Source)
at org.eclipse.swt.widgets.Widget.<init>(Unknown Source)
at org.eclipse.swt.widgets.Control.<init>(Unknown Source)
at org.eclipse.swt.widgets.Scrollable.<init>(Unknown Source)
at org.eclipse.swt.widgets.Composite.<init>(Unknown Source)
at org.eclipse.swt.browser.Browser.<init>(Unknown Source)
at BrowserThread.run(BrowserThread.java:30)
Exception in thread "Thread-1" org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.checkWidget(Unknown Source)
at org.eclipse.swt.widgets.Widget.checkParent(Unknown Source)
at org.eclipse.swt.widgets.Widget.<init>(Unknown Source)
at org.eclipse.swt.widgets.Control.<init>(Unknown Source)
at org.eclipse.swt.widgets.Scrollable.<init>(Unknown Source)
at org.eclipse.swt.widgets.Composite.<init>(Unknown Source)
at org.eclipse.swt.browser.Browser.<init>(Unknown Source)
at BrowserThread.run(BrowserThread.java:30)
Any advice?
Thank you
Rob
|
|
|
|
|
Re: Many browser widgets in many threads [message #508874 is a reply to message #508804] |
Wed, 20 January 2010 10:46  |
Eclipse User |
|
|
|
You can use Display.timerExec() to execute something after a specified
number of milliseconds has passed, for an example of this see
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet16.java?view=co .
The native browsers generally should not wait for a page rendering to
complete if a setUrl() or setText() is issued in the meantime, though it's
ultimately up to the native browser implementation to handle this. If you
want to be notified of when a page load completes then you can add a
ProgressListener to the Browser and its completed() method will be invoked.
Grant
"nysalsa" <nysalsa@hotmail.it> wrote in message
news:hj6qjd$1g2$1@build.eclipse.org...
> Thank you Grant, I understood in another way that part of the
documentation (my fault... sorry).
> This drives a question: having to simulate many user page-requests with
pre-defined gap between then (i.e. 4 secs between the first and the second
one) how can I do that ?
> I read that the page rendering is asynchronous but it seems that that
second request waits for the complete page rendering of the first one.
> What I'm missing ?
> Thank you in advance
>
> Rob
|
|
|
Powered by
FUDForum. Page generated in 0.04875 seconds