Cancel Zip save method

127 views Asked by At

I just wonder is there a way to cancel long running methods with cancellation token?
My problem with zip.Save(), I wanna cancel by press button.

I am thinking about thread abort, but not sure.

string path = Server.MapPath("~/Test/");//Location for inside Test Folder
string[] Filenames = Directory.GetFiles(path);
using (ZipFile zip = new ZipFile())
{
    zip.AddFiles(Filenames, "Project");//Zip file inside filename
    zip.Save(@"C:\Users\user\Desktop\Projectzip.zip");
}
1

There are 1 answers

0
UdoQQ On BEST ANSWER

For someone who will also google this You can process cancellation at zip.SaveProgress

Just add something like this

                    zip.SaveProgress += (sender, args) =>
                    {
                        if (worker.CancellationPending)
                        {
                            args.Cancel = true;
                            return;
                        }
                    };