I want to subscribe and unsubscribe from a ToolStripButton's Click event using a globally declared delegate as discribed in this SO question and subsequent answers. I have the following delegate declared globally in my class
private delegate void StopTask(ref MainForm _mainForm,
Task _task, CancellationTokenSource _cancelSource);
I then set this delegate to hold a method as follows
StopTask cancelTask = UtilsTPL.CancelRunningProcess;
where in the public class UtilsTPL, CancelRunningProcess is defined as
public static void CancelRunningProcess(ref MainForm _mainForm, Task _task,
CancellationTokenSource _cancelSource)
{
// Some really amazing code...
}
I then try to add the delegate to my ToolStripButton click event like this (and also unsubscribe as shown)
mainForm.stopButton.Click += cancelTask;
mainForm.stopButton.Click -= cancelTask;
This clearly does not work, as there is a glaring type miss-match.
How do I subscribe/unsubscribe the consistant delegate cancelTask to the buttons click event? Thanks for your time.
Note: I am doing this so that the button can be reused for the cancellation of many different Tasks.