Solved

Create/Trigger Event in a custom C#.Net DLL (Not a Control or Form)

Posted on 2013-05-10
4
1,638 Views
Last Modified: 2013-05-13
Hi Experts!  o/

I have written a DLL that interfaces an external machine, and has reason to return Event information sometimes.  It has been posited that we add an Event to the DLL in order to get the Utility/Controller Application's "attention" when one of these Events occur - be it to display a warning, take automatic corrective action, or whatever.  I thought it sounded like a good idea, and since I've used Events successfully in other Forms and Custom Controls - I figured it would be a no-brainer.

I was wrong.  I can be big about it.  But now I've got to figure out how to get Events working with this DLL.

For what it's worth, I've got the Event Handler declared at the top of the system, in with the variable declaration and definitions:
public event EventHandler MonitorStateChanged;                          // When an external force changes the [Monitor] interface value - tell everybody!  (Or anybody who's listening...)

Open in new window


...and later on, down in the code where that particular interface value may change, I have the following code:
                if (MonitorStateChanged != null)
                {
                    MonitorStateChanged(this, EventArgs.Empty);
                }

Open in new window


...I'll figure out how to properly fill/utilize the [EventArgs] class later - we can send it an (Empty) for now.

And here's the problem: it doesn't work - it's not exposed in the client application as something with which I can connect.  Seriously, I type the following:
sysComms.MonitorStateChanged += new EventHandler(monitorStatusChanged);

Open in new window


...and I get the following errors:
Error 1     Invalid token '+=' in class, struct, or interface member declaration
Error 2     Method must have a return type
Error 3     Identifier expected
Error 4     The type or namespace name 'MonitorStateChanged' does not exist in the namespace 'sysComms' (are you missing an assembly reference?)
Error 5     'sysTestSystem.mainForm.monitorStatusChanged(object, System.EventArgs)' is a 'method' but is used like a 'type'

...so it seems that - over in the sysComms64 DLL (from which [sysComms] is derived) it matters not that the event is defined [Public] and triggered internally.

I went back and checked a couple of my Controls, but they seem to work fine, no problem, and this is the exact same method I used there.  Since this is a DLL (as opposed to a Control or Form), is there something else I need to include in "using..." or something?  What have I missed?

Thanks for your time (and patience!)

- The Lurking LongFist
0
Comment
Question by:LongFist
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
topdog770 earned 500 total points
ID: 39156010
Hi Lurking...

The good news is there's something else happening, other than what you've described so far.  It would almost appear that the dll containing your sysCom object isn't referenced.. or more likely, that your project using sysCom isn't referencing the right version of the dll.

This code, doing what I think you've described works perfectly.

In a C# console app:
namespace driverApp
{
    class Program
    {
        static void Main(string[] args)
        {

            theDLL.Class1 c = new theDLL.Class1();

            c.MonitorStateChanged += new EventHandler(c_MonitorStateChanged);

            for (int i = 0; i < 100; i++)
            {
                c.DoWork();
                System.Threading.Thread.Sleep(55);
            }
        }

        static void c_MonitorStateChanged(object sender, EventArgs e)
        {
            // do something here..

            Console.WriteLine("In MonitorStateChanged");
        }
    }
}

Which then references a C# dll:
namespace theDLL
{
    public class Class1
    {
        public event EventHandler MonitorStateChanged;                          // When an external force changes the [Monitor] interface value - tell everybody!  (Or anybody who's listening...)

        public Class1()
        {

        }

        public void DoWork()
        {
            if (MonitorStateChanged != null)
            {
                MonitorStateChanged(this, EventArgs.Empty);
            }
        }

    }
}
1
 
LVL 1

Author Comment

by:LongFist
ID: 39156614
Okay - I didn't see that one coming.  But you are absolutely correct!  It turns out that version 1.0 still exists on the drive, and in a subfolder not often visited.  (Shamefacedly, I should have archived that, too, when I did my cleaning sweep.  But I didn't, and here we are.

I opened up another application under development - one that only ever used that interface - and the event was there, loud and proud.  It even worked...

Kudos (and points) to you - I never would've thought it.  Now I've got something else for which to sweep the DevSystems folders...

- Thanks again!
0
 
LVL 1

Author Closing Comment

by:LongFist
ID: 39156623
Dead-on accurate, simple to read and understand, and indicative of a massive oversight on my part.  [Red face here.]  I guess you can't afford to get too comfortable, or it will invite complacency, which will soon be followed by mishap (or disaster)...
0
 
LVL 12

Expert Comment

by:topdog770
ID: 39161357
it happens to all of us.. we think complex and abstract and sometimes it's just the semi-colon that gets in the way
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question