Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to raise an event when another event is raised?

Tags:

c#

events

I have an application that handles the OnQuit event of another running application. How can I raise an additional (custom) event when said OnQuit event is handled.

My OnQuit handler:

private void StkQuit()
{
   _stkApplicationUi.OnQuit -= StkQuit;
   Marshal.FinalReleaseComObject(_stkApplicationUi);
   Application.Exit();
}
like image 654
wulfgarpro Avatar asked Nov 05 '22 04:11

wulfgarpro


1 Answers

I will usually have an event in my view interface like so:

public interface ITestView
    {
        event EventHandler OnSomeEvent;
    }

Then from a presenter constructor I'll wire up those events:

public class TestPresenter : Presenter
{
    ITestView _view;

    public TestPresenter(ITestView view)
    {
        _view.OnSomeEvent += new EventHandler(_view_OnSomeEvent);
    }

    void _view_OnSomeEvent(object sender, EventArgs e)
    {
        //code that will run when your StkQuit method is executed
    }
}

And from your aspx codebehind:

public partial class Test: ITestView
{
     public event EventHandler OnSomeEvent;
     public event EventHandler OnAnotherEvent;

    private void StkQuit()
    {
        _stkApplicationUi.OnQuit -= StkQuit;
        Marshal.FinalReleaseComObject(_stkApplicationUi);
        if (this.OnSomeEvent != null)
        {
            this.OnSomeEvent(this, EventArgs.Empty);
        }
        Application.Exit();
    }
}

Hope that helps!!

like image 189
Cognitronic Avatar answered Nov 09 '22 11:11

Cognitronic