Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7652363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:44:57+00:00 2026-05-31T11:44:57+00:00

I want to subscribe and unsubscribe from a ToolStripButton ‘s Click event using a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:44:59+00:00Added an answer on May 31, 2026 at 11:44 am

    Instead of subscribing and unsubscribing from the click event, call your cancelTask from your click-event

    private void stopButton_Click(object sender, EventArgs e)
    {
        var handler = cancelTask;
        if (handler != null) {
            handler(this, _task, _cancelSource);
        }
    }
    

    and subscribe and unsubscribe from cancelTask

    cancelTask += Whatever;
    cancelTask -= Whatever;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We want to put a link on our site Subscribe, when you click this
I have a ValueChanged event. I want to subscribe to it using Rx only
I want to raise an event from a .NET class, and receive this event
In my class I want to declare an event that other classes can subscribe
Consider the situation in which you want to subscribe to an event for one
I have a function that I want to subscribe it to a event queue.
I have a model class with an event which I subscribe from other classes.
I'm creating a number of UserControl s dynamically using LoadControl(String) and want to subscribe
I want to show a subscribe/unsubscribe link on a forum topic list page, along
want to know why String behaves like value type while using ==. String s1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.