开发者

Raising events in C# from classes

Basically I am attempting to write a generic interprocess communication implementation between C# and the Android platform (using Java).

Creating the socket stack is simple enough but what I am wondering is what would be a good pattern to passing in delegates into my TcpSer开发者_StackOverflow社区ver class and raising the required events.

The idea so far is.

while (connected) {
    //Compile all the required data and so forth
        switch (currentState)
        {
               case CurrentServerState.EventReceived:
                    //Raise my event...
               break;
               case CurrentServerState.ObjectReceived:
               break;

        }
}

So would the correct implementation simply expose an array of delegates and use reflection to match event names or something along those lines? What are my options? Also how do I make sure that any raised events are raised in the UI thread given that this is all occurring from a thread created for the server?


If you don't need to pass any custom arguments to the event handler you can use the default EventHandler delegate which uses the base EventArgs.

Declare the events you want to make available like this:

public event EventHandler EventReceived;
public event EventHandler ObjectReceived;

Declare a protected method for each event to handle raising the event for you. This is really just a convenience to keep from repeating this each time you need it. You could do this inline.

Your events will be null if there are no attached event handlers. The method handles checking for this before firing the event.

protected void RaiseEventReceived(EventArgs e)
{
    if (EventReceived != null)
        EventReceived(this, e);
}

Then call the method when you want to raise the event. The method will handle checking to see if anyone is listening for the event.

public void SomeOtherMethod()
{
    while (IsConnected)
    {
        switch (CurrentState)
        {
            case CurrentServerState.EventReceived:
                RaiseEventReceived(EventArgs.Empty);
                break;

            case CurrentServerState.ObjectReceived:
                RaiseObjectReceived(EventArgs.Empty);
                break;
        }
    }
}

You can declare your own class to pass custom arguments to the event handlers by deriving from EventArgs such as the following. By convention your class should be named *something*EventArgs.

public class EventReceivedEventArgs : EventArgs
{
    // Declare properties to hold additional event info here
}

Then you would need to declare a custom delegate that takes your new event args parameter:

public delegate void EventReceivedEventHandler(object sender, EventReceivedEventArgs e);

Update your event declarations to use your new delegate type instead of EventHandler:

public event EventReceivedEventHandler EventReceived;

And finally, when raising the event you of course would want to create a new instance of your custom event arguments class and intitialize your custom properties.

Clients can attach multiple handlers to your events using the normal syntax and they will always be called on the correct thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜