简体   繁体   中英

How to raise an event when another event is raised?

I have an application that handles the OnQuit event of another running application. I would like to raise an additional (custom) event when the OnQuit event is handled. How could I implement such an event?

My OnQuit handler is like so:

    private void StkQuit()
    {
        _stkApplicationUi.OnQuit -= StkQuit;
        Marshal.FinalReleaseComObject(_stkApplicationUi);
        Application.Exit();
    }

The reason I require the additional event is so that I can tell my View layer that the application has exited. If this is not the correct way, what would be better?

WulfgarPro

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!!

Just register this additional event with the _stkApplication after OnQuit has been registered.

_stkApplicationUi.OnQuit += StkQuit;
_stkApplicationUi.OnQuitAdditional += AddlQuitHandler;

where AddlQuitHandler is the handler for the custom event

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM