Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

How do you create software events


  • Please log in to reply
5 replies to this topic

#1 alanb

alanb

    Member

  • Members
  • PipPip
  • 28 posts
  • LocationNottingham UK

Posted 17 February 2011 - 06:42 PM

Hi,

So far I have created a display manager class, that internally creates a thread listening on a socket for commands. That all works fine, the main thread generates numbers on a display and sending a message from a client on my pc will display that message on the display.

Now when my displaymanager recieves a command code on the socket I want to pass that command back to the main program. On the pc I would usually use an event. When I looked for EventHandler<> I could only find something called NativeEventHandler. Is that the N+ equivalent of EventHandler?

What is the equivalent of my pc code
public event EventHandler<StringEventArgs> ReceivedCommand;

...

In Some function

if (ReceivedCommand != null)
   ReceivedCommand(this, new StringEventArgs(command));

Or, as is highly probable, am I going about this in the wrong way.

By the way, a lot of this code is by others from the forum so if you recognise your own code, profound thanks. I will reference everyone in the finished article.
Alan

Attached Files



#2 Dan Morphis

Dan Morphis

    Advanced Member

  • Members
  • PipPipPip
  • 188 posts

Posted 17 February 2011 - 07:28 PM

Now when my displaymanager recieves a command code on the socket I want to pass that command back to the main program. On the pc I would usually use an event. When I looked for EventHandler<> I could only find something called NativeEventHandler. Is that the N+ equivalent of EventHandler?


You would indeed use an event, but not in the .NET 2.0 way of using public event EventHandler<SomeEventArg> MyEvent;

The equivilent in NETMF is

public class SomeClass {
    public delegate void MyEventDelegate(object sender, MyEventArgs e);
    public event MyEventDelegate MyEvent;

    public class MyEventArgs : EventArgs {
        ...
    }

    private void OnMyEvent(MyEventArgs e) {
        var handler = MyEvent;
        if (handler != null) {
           handler(this, e);
        }
    }

   public void DoSomething() {
       OnMyEvent(new MyEventArgs());
   }
}


#3 kenNET

kenNET

    Advanced Member

  • Members
  • PipPipPip
  • 69 posts
  • LocationSweden

Posted 17 February 2011 - 07:33 PM

Have a look the changes I did to the RDIF code here: http://forums.netdui..._7220#entry7220 (look after //(** new **)) There I use delegate and event in NETMF.

#4 alanb

alanb

    Member

  • Members
  • PipPip
  • 28 posts
  • LocationNottingham UK

Posted 17 February 2011 - 09:02 PM

Have a look the changes I did to the RDIF code here: http://forums.netdui..._7220#entry7220 (look after //(** new **))

There I use delegate and event in NETMF.


Thanks for the information, now all working correctly. Thanks also to Dan for the same solution.

Alan

#5 VipX1

VipX1

    New Member

  • Members
  • Pip
  • 6 posts
  • LocationDublin, Ireland

Posted 26 February 2012 - 01:00 PM

You would indeed use an event, but not in the .NET 2.0 way of using public event EventHandler<SomeEventArg> MyEvent;

The equivilent in NETMF is


    public delegate void MyEventDelegate(object sender, MyEventArgs e);
    public event MyEventDelegate MyEvent;


:rolleyes:

Thanks guys, that delegate declaration is exactly what I needed and I was scrolling threw various .NetMF sites and blogs but couldn't find it. Then I thought everyone who got a Netduino had this situation...

Am I right in saying that the EventHandler must be executed this way because .NetMF doesn't support Generics?

Cherrs, VipX1.

#6 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 26 February 2012 - 04:04 PM

Hi VipX1, You are correct. Since .NET MF doesn't support generics, you'll want to use the traditional event method built into .NET 1.0+ (as outlined by Dan Morphis). Chris




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.