I am trying to update a label in windows forms. The action is happening in a separate class but while the action is happening on a separate class. The label should be updated, but things seem to be not working. Kindly assist
Below is the Back code of the form ProcessingUI
- 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();
-
- Task.Factory.StartNew(() =>
- {
- while (_isRunning)
- {
-
- var data = _processing.Processdata(lblCounter, _taskToken);
- if (data.Success)
- _isRunning = false;
- if (_taskToken.IsCancellationRequested)
- return;
- }
-
- });
-
- }
-
- public delegate void SetStatusCallback(string message);
- public void UpdateStatus(string message)
- {
- if (this.lblCounter.InvokeRequired)
- {
-
- this.Invoke(new SetStatusCallback(UpdateStatus),
- message);
- }
- else
- this.lblCounter.Text = message;
- }
- }
Then here is a separate class that has the action, basically its just updating. Now on update I just want to pass the record that is being updated. So i call the Method from the form and use it in this class.
- public class Processing
- {
- public Results Processdata(CancellationTokenSource taskToken)
- {
- foreach (var record in dataCases)
- {
-
- new ProcessingUI().UpdateStatus(record.RequestReference);
- }
-
- }
- }