Już Ci pokazuję kod. Ogólnie na początku dodałem private CancellationTokenSource cts;
Klasa, której to dotyczy:
public void Timer1_Tick(object sender, EventArgs e)
{
progressBar1.Increment(20);
if (progressBar1.Value == 100)
{
timer1.Stop();
System.Threading.Thread.Sleep(101);
cts = new CancellationTokenSource();
CancellationToken token = cts.Token;
Task.Factory.StartNew(() =>
{
token.ThrowIfCancellationRequested();
foreach (var obj in listBox1.Items)
{
string dir = Convert.ToString(obj);
listBox3.DataSource = GetFileList("*.*", dir).ToArray();
int count = listBox3.Items.Count;
for (int i = 0; i < count; i++)
{
listBox5.Items.Add(listBox3.Items[i].ToString());
}
}
string filecount = Convert.ToString(listBox5.Items.Count);
textBox1.Text = filecount;
ExecuteSecure(() => label2.Text = "Przeszukano: 0 z " + filecount);
ExecuteSecure(() => label5.Visible = false);
ExecuteSecure(() => timer4.Start());
ExecuteSecure(() => progressBar1.Value = 0);
foreach (var file in listBox5.Items)
{
string obj = Convert.ToString(file);
try
{
ExecuteSecure(() => label4.Text = obj);
if (File.Exists(obj))
{
try
{
using (FileStream scan_obj = new FileStream(obj, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
// System.Threading.Thread.Sleep(23);
if (listBox2.Items.Contains(CheckObject(obj)))
{
ExecuteSecure(() => listBox4.Items.Add(obj));
string foundcount = Convert.ToString(listBox4.Items.Count);
ExecuteSecure(() => textBox3.Text = foundcount);
ExecuteSecure(() => label3.Text = "Liczba znalezionych: " + foundcount);
}
}
}
catch
{
}
}
}
catch
{
}
string filecount2 = Convert.ToString(listBox5.Items.Count);
int scnnr = Convert.ToInt32(textBox2.Text);
scnnr = scnnr * 1 + 1 * 1;
ExecuteSecure(() => textBox2.Text = Convert.ToString(scnnr));
ExecuteSecure(() => label2.Text = "Przeszukano: " + scnnr + " z " + filecount2);
int step = Convert.ToInt32(textBox2.Text);
int final = Convert.ToInt32(textBox1.Text);
int precent = 0;
precent = (step * 100) / (final * 1);
ExecuteSecure(() => textBox4.Text = Convert.ToString(precent));
ExecuteSecure(() => progressBar1.Value = precent);
ExecuteSecure(() => CheckShieldLauncher.Properties.Settings.Default.ScanProgress = Convert.ToString(precent) + "%");
}
}, token);
}
}
Przycisk anulowania:
private void Button1_Click(object sender, EventArgs e)
{
cts.Cancel();
CheckShieldLauncher.Properties.Settings.Default.ScanStatus = "Gotowe!";
CheckShieldLauncher.Properties.Settings.Default.ScanProgress = "0%";
if (textBox3.Text == "0")
{
MessageBox.Show("Przeszukanie przerwane! Nic nie znaleziono.");
this.Close();
}
else
{
System.IO.StreamWriter FoundListGenerator = new System.IO.StreamWriter(Application.StartupPath + "\\Temp\\006.set");
foreach (var obj in listBox4.Items)
{
string obj_loc = Convert.ToString(obj);
FoundListGenerator.WriteLine(obj_loc.ToString());
}
FoundListGenerator.Close();
CheckShieldFoundManager scan_result = new CheckShieldFoundManager();
scan_result.Show();
MessageBox.Show("Przeszukanie przerwane! Podczas skanowania zostały znalezione pożądane obiekty.");
this.Close();
}
}