0
I asked a question earlier to cancel the process of compressing files.
I have 2 problems with this code, the program has the function of compressing files in pairs separately getting 2 files in each 7z, the files are bin,cue using 7z, I have a folder full of these files and wanted to compress them
The problems are:
When I run the program and click cancel the program for but 7z continues in memory working.
When the program finishes I realize that it is still compressing the last file, it seems that the program warns that it has completed but it did not wait for the last task to be completed
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 x;
Process Zip(string file)
{
string game = Path.ChangeExtension(file, null) + ".7z";
string cue = Path.ChangeExtension(file, ".cue");
x = new Process()
{
EnableRaisingEvents = true,
StartInfo = new ProcessStartInfo
{
FileName = "7z.exe",
Arguments = String.Format("a -t7z \"{0}\" \"{1}\" \"{2}\" -mx=9", game, file, cue),
WindowStyle = ProcessWindowStyle.Hidden
}
};
x.Start();
return x;
}
var count = 0;
void Handler(object o, EventArgs a)
{
var p = o as Process;
if (_cancellation.Token.IsCancellationRequested)
{
x.Close();
x.Kill();
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;
}
}
public void Report(int value)
{
Invoke((Action)(() =>
{
progressBar.Value = value;
if (value == 100)
{
btnZip.Enabled = true;
}
}));
}
private void btnZip_Click(object sender, EventArgs e)
{
btnZip.Enabled = false;
_cancellation = new CancellationTokenSource();
var files = Directory.EnumerateFiles(@"C:\Users\Fabyo.GALUTTI\Desktop\p", "*.bin", 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;
}
}
I’ve already solved 99% of the gave problems is your duty to get it working for your specific needs. It clearly demonstrates lack of effort.
– Bruno Costa
Bruno I tried and I am trying, I have made several attempts without success, it is not lack of effort, I am a php programmer, I do not know anything of c#, I want to learn but there are things that for me is more difficult, I do not want anyone to do anything for me, I only asked for a help, or an orientation
– Fabyo Guimaraes
@Brunocosta I’m sorry it seemed like I wasn’t having any effort, but on the contrary, I want to make this program work and learn, I even bought a course at udemy, and I researched a lot, and I tried to use Backgroundworker and it didn’t work, in the tests I did simulating a loop with Sleep works right, cancels normal, but when I run the process with 7z, now it is running straight and ending in seconds or is it not doing what you need to do, I tried to use delegates, Hasexited, but as I said I’m new in c#, but the harder it gets the more I’m enjoying learning
– Fabyo Guimaraes