Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] freeze while requesting a URL


Perhaps the mail server is sluggish, but I've already received Steve's answer to your question.  Did you try Display.syncExec(), which will give you synchronous (one-by-one) execution.  Future questions of this nature should be asked at:
news://news.eclipse.org

You can search for old news postings at:
http://www.eclipse.org/search/search.cgi

-randy



Moritz Angermann <moritz.angermann@xxxxxxx>
Sent by: platform-swt-dev-admin@xxxxxxxxxxx

04/14/2003 04:34 PM
Please respond to platform-swt-dev

       
        To:        platform-swt-dev@xxxxxxxxxxx
        cc:        
        Subject:        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
--




#### swtThreadTest.java has been removed from this note on April 14, 2003 by Randy Hudson

Back to the top