On Tue, 15 Apr 2008 10:07:23 -0700, Phil Townsend <ph*******@yahoo.com
wrote:
I have an application that needs to respond to events that occur outside
of the application itself. My project, called "ShowDetection" declares
the event. I have a console app called "TestEvent" that I would like to
use to test the event handler. Any action in the console app would be
acceptable, such as a keystroke. I am at a loss on how to go about
detecting and raising this event in my Winform. cn anybody help?
Detecting: you need some code that actually reads keyboard input. That's
the code that does the detecting.
Raising: at some point after you've detected the event, you need to invoke
the delegate field that represents the event. The most common pattern for
doing this would be to call a method that looks like this:
void OnMyEvent()
{
MyEventHandler handler = MyEvent;
if (handler != null)
{
handler(this, new MyEventArgs());
}
}
where "MyEvent" is the name of the event that is declared using the
standard .NET signature (i.e. taking two parameters, the first an object
representing the sender, and the second a class containing the event
data). In that pattern, "MyEventHandler" is the delegate type used for
the event and "MyEventArgs" is the class containing the event data (of
course, usually you'd pass that data to the constructor or otherwise
initialize the class before invoking the event handlers).
The important thing to keep in mind is that in this context, "event"
doesn't refer to some special signaling mechanism supported by the
framework (a connotation that the term would have in other contexts).
It's really just a built-in way of representing a collection of delegates
that are called by a class at a specific point in time. Other than
managing the collection of delegates, everything else needs to be
implemented by the class defining the event, and the invocation of the
handlers is little more than a short-hand for enumerating all of the
handler delegates that were added (subscribed) to the event, calling each
one at a time.
Beyond that, I recommend you read about events in the MSDN documentation,
and if and when you have more specific questions about how to use them,
please feel free to post those there and we can try to answer them.
Pete