Aborting a thread is a very dangerous thing to do, and should be avoided at all costs.
You're using a
BackgroundWorker
, which already runs the work on a background thread. Starting
another thread to do the work, and then freezing the initial background thread until your new thread has finished, will only
decrease the performance of your code.
Forget the manual threading, and do the work in the
DoWork
event handler.
Set
the WorkerSupportsCancellation
property[
^] to
true
. When you want to stop the worker, call
the CancelAsync
method[
^]. Within your loop, check
the CancellationPending
property[
^] to see if the work has been cancelled, and stop if it has.
private void Abort_Click(object sender, EventArgs e)
{
backgroundCopy.CancelAsync();
}
private void backgroundCopy_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
List<object> genericlist = e.Argument as List<object>;
if (genriclist is null || genericlist.Count < 2) throw new ArgumentException();
string source = Convert.ToString(genericlist[0]);
string destination = Convert.ToString(genericlist[1]);
if (backgroundCopy.CancellationPending)
{
e.Cancel = true;
return;
}
}