One relatively simple solution would be to use
the Progress<T>
class[
^], which takes care of the cross-thread access for you.
public class Processing
{
public Results ProcessData(IProgress<string> statusReporter, CancellationToken cancellationToken)
{
foreach (var record in dataCases)
{
statusReporter.OnReport(record.RequestReference);
cancellationToken.ThrowIfCancellationRequested();
}
}
}
public partial class ProcessingUI : Form
{
private void start_Click(object sender, EventArgs e)
{
StartProcessingTask();
}
private void StartProcessingTask()
{
if (_isRunning)
return;
_isRunning = true;
_taskToken = new CancellationTokenSource();
CancellationToken cancellationToken = _taskToken.Token;
IProgress<string> statusReporter = new Progress<string>(UpdateStatus);
Task.Run(() =>
{
while (_isRunning)
{
var data = _processing.ProcessData(lblCounter, cancellationToken);
if (data.Success)
{
_isRunning = false;
}
else
{
cancellationToken.ThrowIfCancellationRequested();
}
}
});
}
private void UpdateStatus(string message)
{
lblCounter.Text = message;
}
}