To kill the Voce process you can call process.Kill();
Yet the trick is in not calling WaitForExit
since this method will wait until the process ends and this way will also block your graphic interface.
In other words Voce has to subscribe to the event Exited
.
Due to the functioning of 7zip
Voce has the restriction to release only one process at a time to add an archive to the zip file. This has implications in terms of code. You cannot simply scroll through the files. You have to re-launch the process only when the previous process is finished.
Each time a file is added Voce can also have a progress bar. This progress notification mechanism will also serve to determine when the process of adding all the files to the zip has completed.
In this particular algorithm I do not allow the cancellation of the process itself. I simply stop adding files to the archive.
What this means is if Voce has quite large files then the cancellation will only be processed once that file is added.
One way to do this is this::
public partial class Form1 : Form, IProgress<int>
{
private CancellationTokenSource _cancellation;
public Form1()
{
InitializeComponent();
}
private void ZipFiles(IList<string> files, IProgress<int> progress, CancellationToken token)
{
Process Zip(string file)
{
var cue = Path.ChangeExtension(file, ".cue");
var process = new Process()
{
EnableRaisingEvents = true,
StartInfo = new ProcessStartInfo
{
FileName = "7z.exe",
Arguments = $"a zip.7z {file} {cue}",
WindowStyle = ProcessWindowStyle.Hidden
}
};
process.Start();
return process;
}
var count = 0;
void Handler(object o, EventArgs a)
{
var p = o as Process;
if (_cancellation.Token.IsCancellationRequested)
{
return;
}
if (count < files.Count)
{
var next = Zip(files[count]);
count++;
next.Exited += Handler;
}
progress.Report(count * 100 / files.Count);
p.Exited -= Handler;
}
{
var process = Zip(files[count]);
process.Exited += Handler;
}
}
private void btnZip_Click(object sender, EventArgs e)
{
btnZip.Enabled = false;
_cancellation = new CancellationTokenSource();
var files = Directory.EnumerateFiles(@"C:\code", "*.cs", SearchOption.AllDirectories)
.Take(100)
.ToList();
ZipFiles(files, this, _cancellation.Token);
}
private void btnCancel_Click(object sender, EventArgs e)
{
_cancellation.Cancel();
progressBar.Value = 0;
btnZip.Enabled = true;
}
public void Report(int value)
{
Invoke((Action)(() =>
{
progressBar.Value = value;
if (value == 100)
{
btnZip.Enabled = true;
}
}));
}
}
The best thing would be to put all the files you want to archive inside a structure, since the 7zip
allows you to archive the files of a board. That way your code would be simpler.
7z a c:\archive3.zip dir2\dir3\
But in case you need different directories then the code I provided should solve.
So but my application it does not answer I click on the button and does not let click is as if I have not answered, I believe that it should be the Waitforexit that does it, stop the process itself I know how to do but the problem is that it does not let press the button and if I do not use the Waitforexit it does not work and does not compact the files
– Fabyo Guimaraes