1

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");
}
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
UdoQQ
  • 45
  • 5
  • 1
    Are you using Ionic Zip? – ProgrammingLlama Apr 20 '22 at 09:02
  • https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/task-cancellation – Abdelhakim Apr 20 '22 at 09:03
  • yes, i can use another lib if it has supporting of cancellation token – UdoQQ Apr 20 '22 at 09:03
  • 1
    Abdelhakim, i think this cant help because all examples uses cancellation token in some sort of loop. I just have problem with one long running method - Save() – UdoQQ Apr 20 '22 at 09:06
  • 2
    It's not that. You should add appropriate tags to your question, as otherwise we have to try and guess what library you're using (potentially guessing incorrectly). – ProgrammingLlama Apr 20 '22 at 09:07
  • Have a look at [GZipStream](https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.gzipstream?view=net-6.0) – McNets Apr 20 '22 at 09:09
  • @McNets GZipStream only supports storing a single file, hence would require additional archiving on top to achieve plain Zip functionality. – orhtej2 Apr 20 '22 at 09:13
  • 1
    I would suggest posting on https://softwarerecs.stackexchange.com/ instead. There is no way to cancel the save without explicit cooperation by the zip library. So unless you want to write your own library it becomes an issue of finding a suitable library with such support. – JonasH Apr 20 '22 at 09:32
  • 2
    As [the Ionic Zip package page says](https://www.nuget.org/packages/Ionic.Zip/) : `This package has been deprecated as it is legacy and is no longer maintained.`. – Panagiotis Kanavos Apr 20 '22 at 11:26

1 Answers1

2

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;
                        }
                    };
UdoQQ
  • 45
  • 5