Question

I have a background worker with the task of downloading large numbers of small files and saving them on the hard disk. To give you an idea of scale, 30 million files.

The background worker is cancellable and I can cancel just fine once I begin downloading the files. But I can't cancel during the call to the external dll method which tells me what files to download. I have an external dll with a method I call which returns this long list of files. It can take hours to complete just that one single method.

I really need to be able to cancel this call. For example, the user gets frustrated with the time it's taking and hits cancel. Right now, I have the wait until that method completes before I can cancel.

Here is an example:

void worker_DoWork(object sender, DoWorkEventArgs e)
{
    TimeIntensiveMethod(); // Can take up to an hour - can't cancel it

    for (int i = 0; i < 1000; i++)
    {
        if (worker.CancellationPending)
            break;

        ShortDurationMethod(); // The wait to complete before cancelling is unperceivable
    }
}

In this example, I want to be able to cancel on the TimeIntensiveMethod while it's in the middle of working. Is this possible?

Was it helpful?

Solution

The only more or less reliable way to cancel the execution of native code, is to run it in a separate process, and to stop that process.

That will work reliably only if that process doesn't do I/O though. If it does, who knows what happens if it is interrupted in the middle of an I/O operation.

There are several ways to run the call in a separate process, and to marshal the parameters and return values across the process boundary. Using WCF is one option. Using the managed add-in framework is another one (though I would not recommend it). Running a console program using the System.Diagnostics.Process class and capturing the output stream is probably the best option.

OTHER TIPS

As a simple and care-free alternative to out-of-process call, you can simply start a new thread, make the call in it, and terminate that thread from your main thread if you feel like it.

You'll have some resource-leakage, but if it is an user-driven app, it might be acceptable...

EDIT:

One other method would be to BREAK the process intentionally from behind, for example if it is file enumeration, lock some files to make enumeration fail and exit...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top