You do not need to pass CancellationTokenSourcein DoProcessing, but simply CancellationToken.
To handle cancellation, you can do something like this:
public void DoProcessing(CancellationToken token, IProgress<string> progress)
{
foreach(excelrow row in excelrowlist)
{
if(token.IsCancellationRequested)
break;
progress.Report("Processed x number of records. please wait..");
}
}
, btnStart_Click. , :
CancellationTokenSource tokenSource;
private void btnStart_Click(object sender, RoutedEventArgs e)
{
txt_importStatus.Text = "";
var progress = new Progress<string>(progress_info =>
{
txt_importStatus.Text = progress_info + Environment.NewLine + "Please dont close this window while the system processing the excel file contents";
});
tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
await Task.Run(() => DoProcessing(token, progress));
}