Cancel button
I have a GUI where the user can set a bunch of options, then when he is done, he can click the go button and it starts a recursive function that crawls a webpage.
I want to implement a cancel button so that when the user decides he has had enough of waiting, he can halt the function. Unfortunately, as soon as the function starts, the GUI stops responding. How can I keep the GUI running while the function runs?
[427 byte] By [
111axa] at [2007-11-27 5:10:24]

# 1
Long running code should be executed in a separate Thread, so you don't block the GUI.
# 2
try implementing the recursive call using SwingUtilities.invokeLater
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)
public void recurse( final Object param ) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
if( !quitRecurse ) {
recurse( param );
}
}
} );
}
Your cancel button should set the quitRecurse flag
# 3
No, you don't implement the recursive call by using SwingUtilities.invokeLater(...).
The invokeLater(...) method places all the code on the GUI event Thread so the GUI can't repaint itself.
You need a separate Thread for the recursive function. Within that code, if you need to update the GUI then you use the invokeLater(...) method.
# 4
good point, instead of invokeLater try
public void recurse( final Object param ) {
new Thread(
new Runnable() {
public void run() {
if( !quitRecurse ) {
recurse( param );
}
}
} ).start();
}
# 5
There is no need to start multiple threads for each recursion level. You start a single Thread and build the recursion into the Thread itself.
# 6
that is a more optimal solution, but mine should work, too -- 10 lines of code for 10 dukes?
# 7
> that is a more optimal solution, but mine should
> work, too -- 10 lines of code for 10 dukes?
Instead of worrying so much about dukes, why not try to write better programs? I'll happily give you 10 dukes if you promise never to post such a silly code snippet here ever again.
# 8
new Thread(
new Runnable() {
public void run() {
crawlr.start();
goButton.setEnabled(true);
}
});
goButton.setEnabled(false);
That code doesn't seem to work; the crawler.start function never gets called. It is inside the event listener for gobutton. crawlr.start() calls the recursive function.
111axa at 2007-7-12 10:30:29 >

# 9
whoa there Torgil, I assure you my programs are fineawfully bold for someone with almost 2000 posts and <500 points, says something about the adviceI'll take you up on those points if you can tell me what is so silly
# 10
you need to start() the new Thread, see other post
# 11
new Thread(
new Runnable() {
public void run() {
crawlr.start();
goButton.setEnabled(true);
}
}).start(); // start thread here
goButton.setEnabled(false);
you might have an issue with your goButton enabling, sometimes on, sometimes off
willing to post the crawlr and goButton code?
Message was edited by:
developer_jbs
# 12
> I assure you my programs are fineNo, there aren't and I aleady told you why.
# 13
> whoa there Torgil, I assure you my programs are fine
Perhaps so, but judged from your suggestions here I'm not convinced.
> awfully bold for someone with almost 2000 posts and
> <500 points, says something about the advice
As most people here, I don't give a **** about duke dollars. So, for instance, I'm not hurrying to post badly thought out solutions to people's problems just so that I can get more points. You do know that they are worthless?
> I'll take you up on those points if you can tell me
> what is so silly
camickr has already told you what is silly about your code. Why on earth are you starting a new thread for each level in the recursion? If you can explain that to me, then I might take back what I said.
# 14
> > awfully bold for someone with almost 2000 posts
> and
> > <500 points, says something about the advice
>
> As most people here, I don't give a **** about duke
> dollars. So, for instance, I'm not hurrying to post
> badly thought out solutions to people's problems just
> so that I can get more points. You do know
> that they are worthless?
>
In fact, your replies #4 and #6 in this thread is a perfect example of why the duke reward system is, on the whole, worthless.
# 15
Hey, that worked! Thank you, developer jbs.
111axa at 2007-7-21 21:20:57 >

# 16
to be fair, you implemented camickr's solution
# 17
ax, thanks for the dukes!
Torgil, having given myself the night to "let it go" , i haven't...
> Perhaps so, but judged from your suggestions here I'm
> not convinced.
i wasn't trying to convince you of anything, i wanted the points
> As most people here, I don't give a **** about duke
> dollars. So, for instance, I'm not hurrying to post
> badly thought out solutions to people's problems just
> so that I can get more points.
ahh an idealist...you don't make much $ do you? keep taking your time
my hunch is that most people post without too much thought, after all these are discussion forums not specification requests
> You do know
> that they are worthless?
ahh a liberal (communist?)...you don't make much $ do you? you do know that you are debunking some fundamental principals of capitalism?
> > I'll take you up on those points if you can tell me
> > what is so silly
competitive, opportunistic, and adaptive
>
> camickr has already told you what is silly about your
> code.
camickr didn't tell me what was silly, he told me/us (others gained insight due to my mistake) what was wrong with using invokeLater and offered sound (general) advice about the recursion
> Why on earth are you starting a new thread for
> each level in the recursion?
the post captures only a portion of the whole problem
hypothetically, imagine a case where discovery, finding the next link, is more pertinent than processing, storing/caching/indexing, its contents
given a single recursive function, its seems reasonable to recurse (asynchronously) upon discovery of a link then process its contents
in this case a threadpool may be better suited than starting a new thread per recurse or using a single thread
this scales horizontally -- add more machines, crawl faster, add enough machines (e.g. Google) run the web
> If you can explain that
> to me, then I might take back what I said.
you can't take it back, it was written and here it is/has been for the world to read
if each reader represents a program and readingYourPosts is an algorithm, what happens if you change the input after it's called? (yes, i'm a d-o-r-k using a software analogy)
Torgil, about those dukes, now this is silly, keep em (you don't earn that many)
# 18
Haha, at least your contributions in this thread have gone from being worthless to being amusing. That's a step in the right direction. :-)(My own posts in this thread has stayed on the worthless level, so you're one up on me there.)