Solved

Events vs Message Map

Posted on 2010-09-13
9
888 Views
Last Modified: 2013-11-27
Hi there,

ok I think this is short and simple. I am looking at events and delegates in C#. So far so good, in that it seems that you create an Event in a class and add Delegates to it through a public interface that you want called when the event occurs.

So what I want to know... is the message map and WM_Mouseclick etc all gone now... is this the only way to manage events in c# windows forms.

ps.. know of any nice tutorials...

thanks M
0
Comment
Question by:MickyMc
  • 4
  • 3
  • 2
9 Comments
 
LVL 2

Expert Comment

by:EdMonk
ID: 33660963
I'm not too sure exactly what you are asking. Could you provide an example of what problem you are trying to solve or can't do?

You can override the WndProc process from within a form to trap messages.

Perhaps these links will help:
http://support.microsoft.com/kb/320584
http://www.codeproject.com/KB/dotnet/devicevolumemonitor.aspx

Here is a link to the windows message constants - http://www.pinvoke.net/default.aspx/Constants/WM.html
0
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 250 total points
ID: 33661027
What you're talking about is at the very core level. With the evolution of Programming languages like C#, VB, Java. These messaging have been encapsulated through different means such as Delegate/Events.

If you wish to handle these messages yourself you can still rely on Win32API programming within C#. Or if you wish to trap the messages for studying you can IMessageFilter

http://msdn.microsoft.com/en-us/library/system.windows.forms.imessagefilter_members(v=VS.90).aspx
0
 

Author Comment

by:MickyMc
ID: 33661073
thanks Ed,

yes I agree I asked a very bad and vague question but you are on the right track. I come from a C++ MFC background and typically deal in messages. If you wanted to notifify other controls of an event you could raise a custom message and fire it.  Controls that where interested would overide the message map and catch the messages they where interested in, otherwise pass them on.

What I want to know is in the case of custom events does the Delegates and Event mechanism replace this message map as in your example of mouse downs etc. I can see how you still use the WM messages for keys, mouse clicks etc but I'm only interested in custom messages.

So as an example of a problem. I want to fire an event in an application to let other controls know about it. I dont know if I do a custom message and let controls listen to the message handlers or use the delegates and events. As you can see Ed, I'm rusty on all this... to much time spent managing and now I'm back to good old bread and butter coding...help!!!

ps.. heres the tutorial I was following...  http://www.geekpedia.com/tutorial236_Delegates-and-Events-in-Csharp.html

rgds Mick,
0
 
LVL 2

Assisted Solution

by:EdMonk
EdMonk earned 250 total points
ID: 33661496
Ah OK I see. Ignore my previous comment as like Novice_Novice said, its at the core level and not what you are wanting.
That guide is correct although a little bit confusing. You are correct, events and delegates should be use to signal events.

I've written a very simple example which includes how to pass parameters back to the subscribing class. The output will be as follows
JobId1 raised an event: Job started
2
JobId1 raised an event: Job Ended

The reason OnJobStart and OnJobEnd events are checked to be null is because if there are no subscribers to the event, it will be null.

I hope this helps.
class Program
{
	static void Main(string[] args)
	{
		Job myJob = new Job();

		myJob.OnJobStart += new Job.JobHandler(myJob_OnJobStart);
		myJob.OnJobEnd += new Job.JobHandler(myJob_OnJobEnd);

		myJob.DoWork();
		Console.ReadLine();
	}

	static void myJob_OnJobStart(object myObject, JobArgs args)
	{
		Console.WriteLine(string.Format("JobId" + args.JobId + " raised an event:" + args.Message));
	}

	static void myJob_OnJobEnd(object myObject, JobArgs args)
	{
		Console.WriteLine(string.Format("JobId" + args.JobId + " raised an event:" + args.Message));
	}
}

class Job
{
	public delegate void JobHandler(object myObject, JobArgs args);
	public event JobHandler OnJobStart;
	public event JobHandler OnJobEnd;
	public void DoWork()
	{
		int jobId = 1;
		//define your arguements
		JobArgs jobArgStart = new JobArgs(jobId, "Job started");
		//fire the on job start event 
		OnJobStart(this, jobArgStart);
		
		//do some work
		int x = jobId + jobId;
		Console.WriteLine(x);

		JobArgs jobArgEnd = new JobArgs(jobId, "Job Ended");
		//fire the on job end event 
		OnJobEnd(this, jobArgEnd);
	}
}
class JobArgs
{        
	public int JobId { get; set; }
	public string Message { get; set; }

	public JobArgs(int jobId, string message)
	{
		this.JobId = jobId;
		this.Message = message;
	}
}

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:MickyMc
ID: 33661873
thanks Ed and Novice...

great example Ed, actually one of the simplest and best I,ve come across.  Novice bang on the money with the message filters etc.

So am I correct in that I should use the Message filter etc for handling windows messages, like mouse clicks, Windows draw, etc and use the Events and delegates for custom notifications. Just trying to work out when I should use them.... rgds Mick
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33662089
Not exactly.

You wish to use IMessageFilter only if you're writing an application which is interested in such message say an application like Mercury QTP. But the caveat with C# is that, using IMessageFilter can only listen to your application messages but not others say Notepad.

For events like Mouse Clicks, Windows Draw and such there's always events associated with the Window or Control. You can rely on those events, you need not take the pain of trapping raw messages.
0
 

Author Comment

by:MickyMc
ID: 33662194
thanks lads for all the help...

is it ok to split the points as both answers really helped.

Novice would you have a link to any guidelines like you mentioned as to when to use the different mechanisms... not to worry if you dont... thanks Mick
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33662222
not exactly. I'd written my own filters while I was automating a UI for testing.

No issue with splitting.
0
 

Author Closing Comment

by:MickyMc
ID: 33662583
thanks Lads... oh Ed... just noticed that you missed the null check in the example... but you did mention it alright... great example... thanks MIck
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now