Events Managed in C #

I am creating an event-driven class, so when I pass it a series of data, it will process and then return the value when ready.

Below is the code that I am using at the moment below, however it is rather nasty, and I'm not sure if it could be easier than that.

   public delegate void MyEventHandler(double result);

    public static MyEventHandler EventComplete;

    public static void MakeSomethingHappen(double[] data)
    {
        ThreadPool.QueueUserWorkItem(DoSomething, data);
    }
    private static void DoSomething(object dblData)
    {
        InvokeEventComplete(AndSomethingElse((double[])dblData));
    }

    private static void InvokeEventComplete(double result)
    {
        if (EventComplete != null)
        {
            EventComplete(result);
        }
    }

    public static double AndSomethingElse(double[] data)
    {
         //do some code
         return result; //double
    }

In my main class, I just hook a method to an event like this,

MyClass.EventComplete += new MyClass.EventCompleteHandler(MyClass_EventComplete);
+3
source share
3 answers

Here you are:

  • An open event as an actual event, not a public member delegate.
  • Fixed additional delegate delegation and general delegate action.
  • Excluded the extra call function, which was just verbose.
  • The lambda expression used to register the event.

:

MyClass.EventComplete += (result) => Console.WriteLine("Result is: " + result);

public class MyClass
{
    public static event Action<double> EventComplete;

    public static void MakeSomethingHappen(double[] data)
    {
        ThreadPool.QueueUserWorkItem(DoSomething, data);
    }

    private static void DoSomething(object dblData)
    {
        var result = AndSomethingElse((double[])dblData);

        if (EventComplete != null)
        {
            EventComplete(result);
        }
    }

    public static double AndSomethingElse(double[] data)
    {
        //do some code
        return result; //double
    }
}
+3

, ...

.NET EventHandler<T> where T : EventArgs, , EventArgs . , , .

public static MyEventHandler EventComplete = delegate {};
//using a no-op handler like this has implications on Garbage Collection

lambda no-op , GC?

if (EventComplete!= null) , , Invoke... .

MyClass.EventComplete += new MyClass.EventCompleteHandler(MyClass_EventComplete);
to
MyClass.EventComplete += MyClass_EventComplete;

, . , static ConsoleApplication: -)

+1

try using a standard event pattern (thousands of times in FCL)

// in [CompleteEventArgs.cs] file
public class CompleteEventArgs : EventArgs {
    private readonly double _result;

    public CompleteEventArgs(double result) {
        _result = result;
    }

    public double Result {
        get { return _result; }
    }
}

// inside your class

// don't forget 'event' modifier(!) it prevents lots of illegal stuff
// like 'Complete = null' on the listener side
public static event EventHandler<CompleteEventArgs> Complete;

public static void MakeSomethingHappen(double[] data) {
    ThreadPool.QueueUserWorkItem(DoSomething, data);
}
private static void DoSomething(object dblData) {
    OnComplete(new CompleteEventArgs(AndSomethingElse((double[])dblData)));
}

// if you're working with a 'normal' (non-static) class
// here should be 'protected virtual' modifiers to allow inheritors
// use polymorphism to change the business logic
private static void OnComplete(CompleteEventArgs e) {
    if (Complete != null)
        Complete(null, e); // in 'normal' way here stands 'this' instead of 'null'
                           // this object (link to the sender) is pretty tricky
                           // and allows extra flexibility of the code on the listener side
}

public static double AndSomethingElse(double[] data) {
    double result = 0;
    //do some code
    return result; //double
}
0
source

All Articles