Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

send/post a message to a window

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark - 07 Oct 2007 21:03 GMT
In a standard unmanaged win32 application I can send/post a message to a
window as a communication mean.
Can it be down also in a dot net application?

Regards
Mark
Peter Duniho - 07 Oct 2007 21:42 GMT
> In a standard unmanaged win32 application I can send/post a message to a
> window as a communication mean.
> Can it be down also in a dot net application?

Yes.  A .NET Control-based object still has an underlying window that
receives messages and a window proc that handles these messages.  Your
own code can override the WndProc method in the class if it has a need
to deal with custom messages.

That said, the kinds of things that may have been done using SendMessage
or PostMessage are often better done using some other mechanism in .NET.
 Occasionally you may indeed want to use the "old-style" mechanisms
you're used to, but it would be a mistake to generally try to do
Win32-style programming in .NET (even if .NET is filled with various
behaviors it's inherited from the native Win32 API :) ).  It's better to
think of .NET as a whole new platform, and focus on implementing
behaviors in a way that use the .NET mechanisms.

Pete
MArk - 08 Oct 2007 07:59 GMT
Thanks,
Following your advise, can you indicate what is the similar mechanism for
sending Windows messages in dot net?

Regards

Mark
> > In a standard unmanaged win32 application I can send/post a message to a
> > window as a communication mean.
[quoted text clipped - 15 lines]
>
> Pete
Peter Duniho - 08 Oct 2007 08:21 GMT
> Following your advise, can you indicate what is the similar mechanism for
> sending Windows messages in dot net?

That depends on what you really want to do.  If you literally want to
send a message, you will have to use the regular Win32 Send/PostMessage
mechanism.

My point is that for the kinds of things that messages are often used
for, you just don't do that any more.  For example, you might have used
something messages like EM_SETSEL and EM_REPLACESEL to modify the text
in an edit control.  In .NET, the TextBox control has instance methods
and properties to do work like that (for example, the Select() method to
change the selection and the SelectedText property to change the text in
the selection).

You mention using messages "as a communication means", but that's not
very descriptive.  In a certain sense, ALL window messages are "a
communication means".  So saying that's what you're using messages for
is pretty vague.

If you have some specific "communications" that you are trying to
accomplish, there is likely in .NET a better way to do it that sending a
window message.  But absent some specific information about what you're
really trying to accomplish, it's not really possible to answer the
question.

Pete
Ben Voigt [C++ MVP] - 08 Oct 2007 19:19 GMT
> Thanks,
> Following your advise, can you indicate what is the similar mechanism for
> sending Windows messages in dot net?

SendMessage becomes Control.Invoke(Delegate)
PostMessage becomes Control.BeginInvoke(Delegate)

> Regards
>
[quoted text clipped - 19 lines]
>>
>> Pete
Peter Duniho - 08 Oct 2007 19:57 GMT
>> Thanks,
>> Following your advise, can you indicate what is the similar mechanism for
>> sending Windows messages in dot net?
>
> SendMessage becomes Control.Invoke(Delegate)
> PostMessage becomes Control.BeginInvoke(Delegate)

That's only partly true.

Many examples of where you might use SendMessage in particular are
simply replaced by calling a method directly.  You'd only use Invoke()
in situations when you'd have used a cross-thread SendMessage() in Win32.

IMHO, PostMessage() has a much closer correlation to BeginInvoke(), but
even there I wouldn't say there's a 1-to-1 correlation.  At least some
of the things one might have used PostMessage() for are, again, better
handled just using normal .NET calls.

Pete
Ben Voigt [C++ MVP] - 09 Oct 2007 15:49 GMT
>>> Thanks,
>>> Following your advise, can you indicate what is the similar mechanism
[quoted text clipped - 14 lines]
> the things one might have used PostMessage() for are, again, better
> handled just using normal .NET calls.

The original post asked about SendMessage and PostMessage as a means of
communication.  Which to my small little brain means "between threads",
because I'd have used a direct function call in native code as well if that
was what I wanted.
Peter Duniho - 09 Oct 2007 18:27 GMT
> The original post asked about SendMessage and PostMessage as a means of
> communication.  Which to my small little brain means "between threads",
> because I'd have used a direct function call in native code as well if that
> was what I wanted.

I don't disagree with that application.  But I do think that's an overly
narrow interpretation of "communication".  As I mentioned to the OP, the
problem description, such as it was, was very vague and it's very
difficult to answer vague questions like that in a really useful way.

But I would not limit "communication" to be inter-thread communication.
 I would say that sending messages like WM_SETTEXT, EM_REPLACESEL, etc.
are forms of communication, and they often occur in a single thread.
Likewise, something like WSAASyncSelect() that uses window messages to
handle Winsock communications; again, usually all in the same thread.

I also think that while direct function calls are best where possible,
there are viable means of inter-thread communication that don't use
Send/Post/Invoke/etc. as the mechanism.

I have tried to encourage the OP to clarify what it is exactly he's
trying to replicate in .NET.  With better details, it's a lot easier to
provide a useful reply.  And it may well be that using Control.Invoke()
or Control.BeginInvoke() are in fact the things he's looking for.  But
those apply to very specific needs, and it could just as easily be that
he doesn't need those at all.

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.