Search code examples
c#multithreadingtaskcancellation-tokenionic-zip

Cancel Zip save method


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");
}

Solution

  • 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;
                            }
                        };