For Programmers: Free Programming Magazines  


Home > Archive > Java Help > September 2004 > Cancel Function for Ongoing Process Question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Cancel Function for Ongoing Process Question
Ben Hoffmann

2004-09-21, 4:02 am


I have an application that downloads a series of items from a web site -
with J2SE. I want to implement a cancel function. I'm gathering this
means I need to use threads in order to interupt the download - is this
correct or am I complicating this?

One approach I've used with other languages - like VB6 - is to poll the
GUI - but I gather thats not very good practice in Object Oriented,
Event driven applications. I also can't figure out how to poll a
button for its state anyways - is this possible?

If threads are the correct approach - is there a simple pattern or
example that explains how to do this - without getting into J2EE or
client - server complications - (beyond my expertise at this point)?

Thanks !

- Ben Hoffmann

Chris Smith

2004-09-21, 4:02 am

Ben Hoffmann wrote:
> I have an application that downloads a series of items from a web site -
> with J2SE. I want to implement a cancel function. I'm gathering this
> means I need to use threads in order to interupt the download - is this
> correct or am I complicating this?


In theory, there are two possibilities here: use multiple threads, or
use asynchronous (non-blocking) I/O functions. However, because you are
using a graphical user interface, you are required to perform blocking
operations outside of the event dispatch thread. That means that
although you still have the choice of blocking or non-blocking I/O, you
will need to create a new thread in either case.

> If threads are the correct approach - is there a simple pattern or
> example that explains how to do this - without getting into J2EE or
> client - server complications - (beyond my expertise at this point)?


The basic idea behind it is as follows:

// in some accessible location:

Thread t;

// in an event handler:

t = new Thread(new Runnable() {
public void run()
{
try
{
// code to read from socket
}
catch (InterruptedIOException e) { }
catch (IOException e)
{
handleException(e);
}
}
});

t.start();

And later, to cancel:

t.interrupt();

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com