Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

manually firing the event in c#

Tags:

c#

i want to fire an event manually using c#. For instance, say if i want to fire a Form_closing event of Form A from Form B. How to do it ??

After getting some comments. I think i need to explain more on this.

Since my Form A is reference to a .dll which creates a custom taskbar in the desktop, There is a situation for me to close the that custom taskbar from Form B. i already tried FormA.Close() from Form B. when i do this, the .dll is unloaded from the app domain and due to this the space occupied by the custom task bar is blocked.

But that is not the case when i click the close button in the custom task bar. when i do this the space is freed up.

This is the reason i want to fire the close event of Form A manually from Form B which will solve my issue.

Thanks.

like image 728
Anuya Avatar asked Sep 04 '09 02:09

Anuya


People also ask

How do you manually call an event?

It can then be invoked like this: const string eventName = nameof(Algorithm. Received); DomainEvent @event = new DomainEvent(payload); InvokeEvent(targetObject, eventName, @event);

How do you raise an event?

Typically, to raise an event, you add a method that is marked as protected and virtual (in C#) or Protected and Overridable (in Visual Basic). Name this method On EventName; for example, OnDataReceived .

What is event handling in C sharp?

An event handler, in C#, is a method that contains the code that gets executed in response to a specific event that occurs in an application. Event handlers are used in graphical user interface (GUI) applications to handle events such as button clicks and menu selections, raised by controls in the user interface.

What is event in C language?

Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system generated notifications. Applications need to respond to events when they occur. For example, interrupts. Events are used for inter-process communication.


1 Answers

We did the following in one project:

There was a GlobalNotifier class, which defined events we wanted to use in different modules of the application, like this

public static class GlobalNotifier
{

    public static event VoidEventHandler EnvironmentChanged;

    public static void OnEnvironmentChanged()
    {
        if (EnvironmentChanged != null)
        {
            EnvironmentChanged();
        }
    }
 }

Then, you could raise this event anywhere when you needed to let the rest of the application know that the environment has changed, like this

    GlobalNotifier.OnEnvironmentChanged();

And also you could subscribe to this event wherever you wanted to be notified about the fact that the environment has changed.

    public ReportingService()
    {
        GlobalNotifier.EnvironmentChanged += new VoidEventHandler(GlobalNotifier_EnvironmentChanged);
    }

    void GlobalNotifier_EnvironmentChanged()
    {
        //reset settings
        _reportSettings = null;
    }

So, whenever you changed the environment, you raised the event, and everyone who needed to know about that and perform some actions, was notified. Might be similar to what you need to achieve.

Also, if you need to pass parameters, you can define the event any way you like, basically -

    public static event VoidEventHandler<SomeObject, List<OtherObject>> SomethingUpdated;

    public static void OnSomethingUpdated(SomeObject sender, List<OtherObject> associations)
    {
        if (SomethingUpdated != null)
        {
            SomethingUpdated(sender, associations);
        }
    }

    // ...

    MyClass.SomethingUpdated+= new VoidEventHandler<SomeObject, List<OtherObject>>(MyClass_SomethingUpdated);

    // ...

    void MyClass_SomethingUpdated(SomeObject param1, List<OtherObject> param2)
    {
      //do something
    }
like image 172
Evgeny Avatar answered Oct 27 '22 04:10

Evgeny