[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [platform-swt-dev] freeze while requesting a URL
|
O.k. here is a fixed verion,
the one befode didn't actually do what i want.
While reading your post. I figure i might have misunderstood
asyncExec().
I though it wouls just ass a process to the queue and than, execute it
in queue. thus the process after would kind of wait for that process.
so it this acutally starts a process it might not be that helpful to me,
since i need a One by One execution of processes. ( see code )
so i have a stack with processes and just execute one. when it's
finished, execute the next one. ... [ but withought freezing the main
thread ]
the attached source is a new ( and fixed version ) of the previous
source.
the buttons:
o New Process: Adds a Dummy Process to the Queue ( wich doesn't take so
long you would expirience a freeze [ this was the bug in the previous
posted source] )
o Print Number of Processes: System.out's the current processes in the
queue.
o Exec next: a by hand execution of the next process ( runnable )
o Enable MainQueue: this will make the display loop execute the
proceesees. ( one after the other )
o Disable MainQueue: just disables this setting.
o Quit: Guess what ;)
o Get!: this does create a proceess wich does read in a URL ( so don't
forget the protocol - this is not fools proof!!! and returns it to the
Textfield below ).
So can i do this 'one' by 'one' execution also with 'asyncExec' ? and
does asyncExec spawn a new Thread? or just queue's up a Runnable for the
main loop? [ that's what i was thinking previously ]
regards.
Moritz
On Mon, 2003-04-14 at 22:04, Steve Northover wrote:
> ProcessList is not thread safe. Why have your own queue? Why not just use
> asyncExec()?
>
>
>
> |---------+---------------------------------->
> | | Moritz Angermann |
> | | <moritz.angermann@gmx.n|
> | | et> |
> | | Sent by: |
> | | platform-swt-dev-admin@|
> | | eclipse.org |
> | | |
> | | |
> | | 04/14/2003 03:21 PM |
> | | Please respond to |
> | | platform-swt-dev |
> | | |
> |---------+---------------------------------->
> >---------------------------------------------------------------------------------------------------------------------------|
> | |
> | To: platform-swt-dev@xxxxxxxxxxx |
> | cc: |
> | Subject: Re: [platform-swt-dev] freeze while requesting a URL |
> | |
> >---------------------------------------------------------------------------------------------------------------------------|
>
>
>
>
> Looks like i found a way to do this:
> maybe this is something to post somewhere? ( i don't know if my code is
> written good enough ) but it might help others who have the same
> problem.
> the basic idea is to have a ProcessList wich is a List of ProcessItems.
> and returns on ProcessList.next() the next item in the queue or null.
> the Item is of the type Runnable so it can get directly executed.
> either via Display.getDefault.asyncExec(r) or via r.run in the mainloop,
> if the execution bool's is set.
>
> sorry for so few comments. i hope the code is mostly human readable.
>
> [btw: would like feedback on this pice, coding style, usage of SWT
> right|wrong, etc. - just if you like]
>
> reg.
> moritz
>
> //--- CODE [swtThreadTest.java] ---//
>
> import org.eclipse.swt.*;
> import org.eclipse.swt.widgets.*;
> import org.eclipse.swt.layout.*;
>
> class ProcessItem{
> public ProcessItem prev = null;
> public ProcessItem next = null;
> public Runnable r = null;
>
> public ProcessItem(Runnable r){
> this.r = r;
> }
> }
>
> class ProcessList{
> private ProcessItem first = null;
> private ProcessItem last = null;
> private int Processes = 0;
>
> public int length(){return this.Processes;}
>
> public void add(Runnable r){
> ProcessItem p = new ProcessItem(r);
> if(this.first == null && this.last == null){
> this.first = p;
> this.last = p;
> }else{
> p.prev = this.last;
> this.last.next = p;
> this.last = p;
> }
> this.Processes++;
> }
> public Runnable next(){
> if(this.first != null){
> Runnable r = this.first.r;
> if( this.first != this.last ){
> // first is not the same
> as last,
> // so we just set first to
> the next element.
> this.first =
> this.first.next;
> } else {
> // we just returned the
> last element.
> // now we need to clear
> out the queue.
> this.first = null;
> this.last = null;
> }
> this.Processes--;
> return r;
> }else{
> return null;
> }
> }
> }
>
> public class swtThreadTest {
> static int ProcessNum = 0;
> static boolean DisplayQueueHandlesProcesses = false;
>
> public static void main (String [] args) {
> final ProcessList List = new ProcessList();
> final Display display = new Display ();
> final Shell shell = new Shell (display);
> shell.setText("Using the DisplayQueue for
> execution of queued
> Processes...");
> GridLayout Layout = new GridLayout();
> Layout.numColumns = 3;
> shell.setLayout(Layout);
>
> Button b = new Button(shell,SWT.PUSH);
> b.setLayoutData(new
> GridData(GridData.FILL_HORIZONTAL));
> b.setText("New Process");
> b.addListener( SWT.MouseDown,
> new Listener(){
> public void handleEvent(
> Event e ){
>
> System.out.println("adding Process:" + List.length());
> Runnable r =
> new Runnable(){
>
> public void run(){
>
> System.out.println("Finishing Process");
> }
> };
> List.add(r);
> }
> });
> b = new Button(shell,SWT.PUSH);
> b.setLayoutData(new
> GridData(GridData.FILL_HORIZONTAL));
> b.setText("Print Number of Processes");
> b.addListener( SWT.MouseDown,
> new Listener(){
> public void handleEvent(
> Event e ){
>
> System.out.println("Processes: "+ List.length());
> }
> });
> b = new Button(shell, SWT.PUSH);
> b.setLayoutData(new
> GridData(GridData.FILL_HORIZONTAL));
> b.setText("Exec next");
> b.addListener( SWT.MouseDown,
> new Listener(){
> public void handleEvent(
> Event e ){
> Runnable r =
> List.next();
> if(r != null)
> display.asyncExec(r);
> else
> System.out.println("There is no Process left!");
> }
> });
> b = new Button(shell, SWT.PUSH);
> b.setLayoutData(new
> GridData(GridData.FILL_HORIZONTAL));
> b.setText("Enable MainQueue Usage");
> b.addListener( SWT.MouseDown,
> new Listener(){
> public void handleEvent(
> Event e ){
>
> DisplayQueueHandlesProcesses = true;
> }
> });
> b = new Button(shell, SWT.PUSH);
> b.setLayoutData(new
> GridData(GridData.FILL_HORIZONTAL));
> b.setText("Disable MainQueue Usage");
> b.addListener( SWT.MouseDown,
> new Listener(){
> public void handleEvent(
> Event e ){
>
> DisplayQueueHandlesProcesses = false;
> }
> });
> b = new Button(shell, SWT.PUSH);
> b.setLayoutData(new
> GridData(GridData.FILL_HORIZONTAL));
> b.setText("Quit");
> b.addListener( SWT.MouseDown,
> new Listener(){
> public void handleEvent(
> Event e ){
> shell.dispose
> ();
> }
> });
>
> shell.pack();
> shell.open ();
>
> while (!shell.isDisposed ()) {
> if(DisplayQueueHandlesProcesses){
> Runnable r = List.next();
> if(r != null){
>
> System.out.println("executing runnable");
> r.run();
> }
> }
> if (!display.readAndDispatch ())
> display.sleep ();
> }
> display.dispose ();
> }
> }
> //--- CODE ---//
>
> On Mon, 2003-04-14 at 19:09, Moritz Angermann wrote:
> > well what i've been now working on half the day was
> > to get my URL fetch function into a thread that requests a page
> > and tells me when it's done.
> > I get errors about invalid Thread access, etc.
> >
> > previous to my tryes i had the URL request in the MainThread ( UIThread
> > )
> > and thus it froze my UI each time i requested a URL.
> >
> > finally i kind of switch a boolean on/off. in the MainThread from the
> > URLThread. but this seems not to be right.
> >
> > I think what i'd need to do is to queue up the MainThread for
> > processing.the stuff.
> >
> > Or should i just go the other way round and let the MainThread(UI) queue
> > up my seperat thread?
> >
> > But than again, how do i access the widgets to set the result?
> > will allways return in invalid thread access :*/
> >
> > thanks in advance,
> > moritz angermann
> >
> > _______________________________________________
> > platform-swt-dev mailing list
> > platform-swt-dev@xxxxxxxxxxx
> > http://dev.eclipse.org/mailman/listinfo/platform-swt-dev
> --
>
> _______________________________________________
> platform-swt-dev mailing list
> platform-swt-dev@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/platform-swt-dev
>
>
>
>
> _______________________________________________
> platform-swt-dev mailing list
> platform-swt-dev@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/platform-swt-dev
--
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import java.net.*;
import java.io.*;
import java.util.regex.*;
class ProcessItem{
public ProcessItem prev = null;
public ProcessItem next = null;
public Runnable r = null;
public ProcessItem(Runnable r){
this.r = r;
}
}
class ProcessList{
private ProcessItem first = null;
private ProcessItem last = null;
private int Processes = 0;
public int length(){return this.Processes;}
public void add(Runnable r){
ProcessItem p = new ProcessItem(r);
if(this.first == null && this.last == null){
this.first = p;
this.last = p;
}else{
p.prev = this.last;
this.last.next = p;
this.last = p;
}
this.Processes++;
}
public Runnable next(){
if(this.first != null){
Runnable r = this.first.r;
if( this.first != this.last ){
// first is not the same as last,
// so we just set first to the next element.
this.first = this.first.next;
} else {
// we just returned the last element.
// now we need to clear out the queue.
this.first = null;
this.last = null;
}
this.Processes--;
return r;
}else{
return null;
}
}
}
public class swtThreadTest {
static int ProcessNum = 0;
static boolean DisplayQueueHandlesProcesses = false;
static Text input = null;
static Text output = null;
static boolean RunningThread = false;
public static void main (String [] args) {
final ProcessList List = new ProcessList();
final Display display = new Display ();
final Shell shell = new Shell (display,SWT.RESIZE);
shell.setText("Using the DisplayQueue for execution of queued Processes...");
GridLayout Layout = new GridLayout();
Layout.numColumns = 3;
shell.setLayout(Layout);
Button b = new Button(shell,SWT.PUSH);
b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
b.setText("New Process");
b.addListener( SWT.MouseDown,
new Listener(){
public void handleEvent( Event e ){
System.out.println("adding Process:" + List.length());
Runnable r = new Runnable(){
public void run(){
System.out.println("This is a Dummy Process!");
}
};
List.add(r);
}
});
b = new Button(shell,SWT.PUSH);
b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
b.setText("Print Number of Processes");
b.addListener( SWT.MouseDown,
new Listener(){
public void handleEvent( Event e ){
System.out.println("Processes: "+ List.length());
}
});
b = new Button(shell, SWT.PUSH);
b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
b.setText("Exec next");
b.addListener( SWT.MouseDown,
new Listener(){
public void handleEvent( Event e ){
if(!RunningThread){
final Runnable r = List.next();
if( r != null )
new Thread(){
public void run(){
RunningThread=true;
r.run();
RunningThread=false;
}
}.start();
else System.out.println("There is no Process left!");
}else{
System.out.println("A Thread is allready running!");
}
}
});
b = new Button(shell, SWT.PUSH);
b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
b.setText("Enable MainQueue Usage");
b.addListener( SWT.MouseDown,
new Listener(){
public void handleEvent( Event e ){
DisplayQueueHandlesProcesses = true;
}
});
b = new Button(shell, SWT.PUSH);
b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
b.setText("Disable MainQueue Usage");
b.addListener( SWT.MouseDown,
new Listener(){
public void handleEvent( Event e ){
DisplayQueueHandlesProcesses = false;
}
});
b = new Button(shell, SWT.PUSH);
b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
b.setText("Quit");
b.addListener( SWT.MouseDown,
new Listener(){
public void handleEvent( Event e ){
shell.dispose();
}
});
input = new Text(shell,SWT.RESIZE | SWT.BORDER);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
input.setLayoutData(gd);
input.setText("http://www.g-sicht.de");
b = new Button(shell, SWT.PUSH);
b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
b.setText("Get!");
b.addListener( SWT.MouseDown,
new Listener(){
public void handleEvent( Event e ){
final String url = input.getText();
Runnable r = new Runnable(){
public void run(){
boolean loop = true;
int maxTry = 3;
int i = 0;
String page = null;
//for our quick'n'dirty redirect corretor
String redirectProtocol = null;
String redirectHost = null;
String redirectURL = null;
while(loop && i < maxTry){
i++;
//lets glue in our SessionID and the server.
System.out.println("Requesting :"+ url +"\" ");
Matcher urlSplit = Pattern.compile("(.*?)://(.*?)/.*?").matcher(url);
if(urlSplit.find()){
redirectProtocol = urlSplit.group(1);
redirectHost = urlSplit.group(2);
}
URL u = null;
InputStreamReader isr = null;
try{ u = new URL(url); }
catch( MalformedURLException ex){ System.out.println(ex.toString()); }
try{ isr = new InputStreamReader( u.openStream() ); }
catch( IOException ex){
/*
* Redirection problem.
* Some WEB hacker seem to prefere to redirect to a page instead of a server and a page.
* so we fetch this here as 'no protocol' error.
*
* This is a dirty Hack to get around this!
*/
System.out.println("Error: "+ ex.getMessage());
urlSplit = Pattern.compile("(.*?): (.*)").matcher(ex.getMessage());
if(urlSplit.find()){
redirectURL = urlSplit.group(2);
}
System.out.println("Warning: got Redirected!");
System.out.println("Requesting: "+redirectProtocol+"://"+redirectHost+redirectURL);
try{ u = new URL(redirectProtocol+"://"+redirectHost+redirectURL);}
catch( MalformedURLException ex2){ System.out.println(ex.toString()); }
try{ isr = new InputStreamReader( u.openStream() ); }
catch( IOException ex2 ){ System.out.println(ex.toString()); }
}
BufferedReader bf = new BufferedReader( isr );
String inputLine;
loop = false;
page = "";
try{ while ((inputLine = bf.readLine()) != null) page += inputLine + "\n"; }
catch( IOException ex){ System.out.println(ex.toString()); }
try{ bf.close(); }
catch( IOException ex){ System.out.println(ex.toString()); }
}
final String result = page;
Display.getDefault().asyncExec( new Runnable(){
public void run(){
output.setText(result);
}
});
}
};
List.add(r);
}
});
output = new Text(shell,SWT.RESIZE | SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL );
gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 3;
gd.heightHint = 200;
output.setLayoutData(gd);
shell.pack();
shell.open ();
while (!shell.isDisposed ()) {
if(DisplayQueueHandlesProcesses && !RunningThread){
final Runnable r = List.next();
if(r != null){
System.out.println("executing runnable");
new Thread(){
public void run(){
RunningThread=true;
r.run();
RunningThread=false;
}
}.start();
}
}
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}