From: Kevin M. <qk...@lm...> - 2001-07-19 13:57:45
Attachments:
timertest.py
|
I am trying to use threads so that I can revalidate some panes while performing a series of long tasks, i.e. long task update & revalidate pane long task update & revalidate pane etc. This would be done in a loop. I have tried several things: synchronize module ------------------ This is used with the notify() & wait() methods. While there are seperate threads they are not running concurrently. This is no good, What I need is synchronous threads. A good example at http://groups.yahoo.com/group/jpython/message/1641 swing.Timer ----------- More promising. I started a timer to update the GUI every 5 seconds and ran long tasks as swing.SwingUtilities.invokeLater(task). However, this didn't work. It seems the timer is blocked until the task is completed (demo attached). SwingWorker Class ----------------- ? Can someone point me in the right direction here? Any good examples,links? Any help appreciated, Kevin _________________________________________________ Name/Title : Kevin McNamee, Software Consultant Phone : +46 13 32 1165 E-Mail : kev...@er... |
From: D-Man <ds...@ri...> - 2001-07-19 15:58:54
|
On Thu, Jul 19, 2001 at 03:57:38PM +0200, Kevin McNamee wrote: | | I am trying to use threads so that I can revalidate some panes while | performing a series of long tasks, i.e. | | long task | update & revalidate pane | long task | update & revalidate pane | etc. Ok. You have this loop in a thread (some subclass of java.lang.Thread). When you want to update the GUI you update it via the SwingUtilities class. Ex : class MyThread( java.lang.Thread ) : def run( self ) : for ... : ... # now update the GUI, create an java.lang.Runnable that # does the work class updater( java.lang.Runnable ) : def run( self ) : <update the gui here> if javax.swing.SwingUtilities.isEventDispatchThread() : # we are already in the AWT_EventQueue thread, it is # safe to touch the GUI updater().run() else : # request that the updater be run in the # AWT_EventQueue thread javax.swing.SwingUtilities.invokeLater( updater() ) HTH, -D |
From: Kevin M. <kev...@er...> - 2001-07-23 10:20:45
|
Thanks, your example was very useful. If I call MyThread().run() and I am not in the event-dispatching thread, everything works fine. I can do long tasks and call invokeLater to update the GUI with each result. If I call MyThread().run() when a button is pressed, then isEventDispatchThread() returns true and the updater function is called directly. This is because button events are handled in the event-dispatching thread, but the result is that the GUI is not updated until the button event finishes :( What I am doing wrong? /Kevin On Thu, 19 Jul 2001, D-Man wrote: > On Thu, Jul 19, 2001 at 03:57:38PM +0200, Kevin McNamee wrote: > | > | I am trying to use threads so that I can revalidate some panes while > | performing a series of long tasks, i.e. > | > | long task > | update & revalidate pane > | long task > | update & revalidate pane > | etc. > > Ok. You have this loop in a thread (some subclass of > java.lang.Thread). When you want to update the GUI you update it via > the SwingUtilities class. Ex : > > class MyThread( java.lang.Thread ) : > def run( self ) : > for ... : > ... > > # now update the GUI, create an java.lang.Runnable that > # does the work > class updater( java.lang.Runnable ) : > def run( self ) : > <update the gui here> > > if javax.swing.SwingUtilities.isEventDispatchThread() : > # we are already in the AWT_EventQueue thread, it is > # safe to touch the GUI > updater().run() > else : > # request that the updater be run in the > # AWT_EventQueue thread > javax.swing.SwingUtilities.invokeLater( updater() ) > > > > HTH, > -D > > > _______________________________________________ > Jython-users mailing list > Jyt...@li... > http://lists.sourceforge.net/lists/listinfo/jython-users > _________________________________________________ Name/Title : Kevin McNamee, Software Consultant Phone : +46 13 32 1165 E-Mail : kev...@er... |
From: dman <ds...@ri...> - 2001-07-23 14:53:25
|
On Mon, Jul 23, 2001 at 12:20:38PM +0200, Kevin McNamee wrote: | Thanks, your example was very useful. | | If I call MyThread().run() and I am not in the event-dispatching thread, | everything works fine. I can do long tasks and call invokeLater to update | the GUI with each result. | | If I call MyThread().run() when a button is pressed, then | isEventDispatchThread() returns true and the updater function is called | directly. This is because button events are handled in the | event-dispatching thread, but the result is that the GUI is not updated | until the button event finishes :( | | What I am doing wrong? You want to /start/, not /run/ MyThread from the button event handler. When you start() a thread it actually creates the new thread, then runs it. The run() method doesn't return until your long work is done, but start() returns immediately. HTH, -D |
From: Pavel T. <pau...@ya...> - 2001-07-19 21:44:07
|
Kevin, By running your long task in invokeLater() you explicitly ask for running your task in AWT Event dispatching thread (the same thread that updates GUI). Just call your long task in timer (with no invokeLater) and GUI must update. If you need some data computed by your long tasks to redraw the gui you must synchronize an access to them explicitly. Hope this helps, Pavel --- Kevin McNamee <qk...@lm...> wrote: > > I am trying to use threads so that I can revalidate > some panes while > performing a series of long tasks, i.e. > > long task > update & revalidate pane > long task > update & revalidate pane > etc. > > This would be done in a loop. I have tried several > things: > > synchronize module > ------------------ > This is used with the notify() & wait() methods. > While there are seperate > threads they are not running concurrently. This is > no good, What I need is > synchronous threads. A good example at > http://groups.yahoo.com/group/jpython/message/1641 > > swing.Timer > ----------- > More promising. I started a timer to update the GUI > every 5 seconds and > ran long tasks as > swing.SwingUtilities.invokeLater(task). However, > this > didn't work. It seems the timer is blocked until the > task is completed > (demo attached). > > SwingWorker Class > ----------------- > ? > > > Can someone point me in the right direction here? > Any good examples,links? > > > Any help appreciated, > Kevin > > > > _________________________________________________ > Name/Title : Kevin McNamee, Software Consultant > Phone : +46 13 32 1165 > E-Mail : kev...@er... > > > > from pawt import swing, awt > from java.io import * > from java.lang import Runtime, Runnable > > class GUI: > def __init__(self): > self.timer = None > button = swing.JButton("Start", > actionPerformed=self.do_task) > self.frame = swing.JFrame("Test", > windowClosing=self.do_quit) > self.frame.getContentPane().add(button) > self.frame.setSize(awt.Dimension(100,100)) > self.frame.visible = 1 > > > def do_task(self, event): > if self.timer != None: > self.timer.stop() > self.timer = swing.Timer(1000, repeater()) > self.timer.start() > swing.SwingUtilities.invokeLater(longTask()) > > def do_quit(self, event): > import sys > sys.exit(0) > > class repeater(awt.event.ActionListener): > def actionPerformed(self, evt): > print "Timer Expired" > > class longTask(Runnable): > def run(self): > print "Doing long task" > proc = Runtime.getRuntime().exec('find / -name a') > stdin = proc.getInputStream() > isr = InputStreamReader(stdin) > br=BufferedReader(isr) > line = br.readLine() > while line != None: > line = br.readLine() > print "Finished long task" > > GUI() > > # End of script > __________________________________________________ Do You Yahoo!? Get personalized email addresses from Yahoo! Mail http://personal.mail.yahoo.com/ |