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 8991829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:49:04+00:00 2026-06-15T22:49:04+00:00

public event EventHandler ConstructDesign; public DataGridView dataGrid = new DataGridView(); public FooClass(Action action) {

  • 0
public event EventHandler ConstructDesign;
public DataGridView dataGrid = new DataGridView();
public FooClass(Action action) {
    ConstructDesign+=action;
    dataGrid.DataBindingComplete+=ConstructDesign;
}

public void Launch() {
    ConstructDesign(null, new EventArgs());
}

//IN A COMPLETELY DIFFERENT CLASS:
public void Main(string[] args) {
    var launcher = new FooClass(Fire);
    launcher.Launch();
}

public void Fire(object sender, EventHandlerArgs args...) {
    Console.WriteLine("Fired");
    //and after the first fire, action will be removed from the `ConstructDesign`.
}

So basically what I’m trying to achieve here is how to do the following:
An Action that is added manually through code to ConstructDesign and upon firing, it will removes itself from the event handler, ConstructDesign. any ideas?

  • 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-06-15T22:49:05+00:00Added an answer on June 15, 2026 at 10:49 pm

    I haven’t found a nice way to unsubscribe from the event after the first use. (You could certainly use a reflection-heavy approach, but I doubt the compiler would complain if a refactoring changed the name of an event).

    Here is one that uses only delegates, so the compiler would still serve you well. It may not be as light-weight as you need, but since I took up the challenge for my own edification, I thought I’d share it.

    MyEvent += SingleUseEventHandler<AssemblyLoadEventArgs, AssemblyLoadEventHandler>
       .Create(This_MyEventOccurred);
    

    Where the magic is defined here:

    public class SingleUseEventHandler<TArgs,THandler>
      where TArgs : EventArgs
    {
      public static THandler Create(EventHandler<TArgs> handler)
      {
         var helper = new SingleUseEventHandler<TArgs, THandler>(handler);
         EventHandler<TArgs> h = helper.InvokeIfFirstTime;
         return (THandler)(object)Delegate.CreateDelegate(typeof(THandler), h.Target, h.Method);
      }
    
      public void InvokeIfFirstTime(object sender, TArgs args)
      {
         if (!raised)
         {
            raised = true;
            handler(sender, args);
         }
      }
    
      public SingleUseEventHandler(EventHandler<TArgs> handler)
      {
         this.handler = handler;
      }
    
      bool raised;
      readonly EventHandler<TArgs> handler;
    }
    

    Of course, C# won’t infer the delegate type, so you have to specify it explicitly.

    If the event’s definition is EventHandler, you can use this instead:

    MyEvent += SingleUseEventHandler<SomeEventArgs>.Create(SomeHandlerMethod);
    
    
    public static class SingleUseEventHandler<TArgs>
      where TArgs : EventArgs
    {
      public static EventHandler<TArgs> Create(EventHandler<TArgs> handler)
      {
         var helper = new SingleUseEventHandler<TArgs, EventHandler<TArgs>>(handler);
         return helper.InvokeIfFirstTime;
      }
    }
    

    Here’s an example program:

    class Program
    {
      static event AssemblyLoadEventHandler MyEvent;
      static int callCount;
      static void Main(string[] args)
      {
         MyEvent += SingleUseEventHandler<AssemblyLoadEventArgs, AssemblyLoadEventHandler>
            .Create(Load);
    
         foreach(var assembly in AppDomain.CurrentDomain.GetAssemblies())
         {
            Console.WriteLine("Raising event for " + assembly.GetName().Name);
            MyEvent(null, new AssemblyLoadEventArgs(assembly));
         }
      }
    
      static void Load(object sender, AssemblyLoadEventArgs eventArgs)
      {
         Console.WriteLine(++callCount);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C# I can test for this... public event EventHandler Trigger; protected void OnTrigger(EventArgs
On the MSDN, I have found following: public event EventHandler<MyEventArgs> SampleEvent; public void DemoEvent(string
I have this code for an event handler: rbM.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton
Why would a 'public event EventHandler cccc' be null? I have a class that's
I have seen people define their events like this: public event EventHandler<EventArgs> MyEvent =
Here is the simple example: public event EventHandler CookinDone = delegate{}; public void CoockinRequest(){
public class a { public event eventhandler test; public void RaiseTest(){//fire test} } Is
public sealed class ImgurUpload { public event EventHandler<UploadCompleteEventArgs> UploadComplete; public void PostToImgur(string location, string
We can raise event in two ways: public event EventHandler MyEvent; private void DoSomething()
I have declared a generic event handler public delegate void EventHandler(); to which I

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.