473,748 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows Form Message Loop

Would I be correct in saying that the only way to get a user message into a
Windows form would be to use P/Invoke with [Send/Post]Message?

Of is there some part of the .NET API that I am totally un aware of?

Thanks for any help
Brian
Nov 16 '05 #1
8 16343
Depends on what you want the message to do and why you want it in the loop?

--
Patrik Löwendahl [C# MVP]
cshrp.net - 'Elegant code by witty programmers'
cornerstone.se 'IT Training for professionals'

Brian Keating EI9FXB wrote:
Would I be correct in saying that the only way to get a user message into a
Windows form would be to use P/Invoke with [Send/Post]Message?

Of is there some part of the .NET API that I am totally un aware of?

Thanks for any help
Brian

Nov 16 '05 #2
the message basically is a string for the window to display,
I want to use the loop because in want to implement a prioritised message
loop, i.e., handle any windows message first then handle my user message.

Reason: My user messages will be coming v.fast, So I want to enforce
processing of any windows messages before processing my user message.

thanks
Brian

"Patrik Löwendahl [C# MVP]" wrote:
Depends on what you want the message to do and why you want it in the loop?

--
Patrik Löwendahl [C# MVP]
cshrp.net - 'Elegant code by witty programmers'
cornerstone.se 'IT Training for professionals'

Brian Keating EI9FXB wrote:
Would I be correct in saying that the only way to get a user message into a
Windows form would be to use P/Invoke with [Send/Post]Message?

Of is there some part of the .NET API that I am totally un aware of?

Thanks for any help
Brian

Nov 16 '05 #3
Seems like you are not completely clear how windows messages are handled in
Windows (Win32) and as such Windows.Forms.

Consider the example you posted recently:
In this you have:
- a main thread handling the UI and,
- two worker threads (called workers), handling external communication and
dispatching messages (Text) to the UI thread.

Because the workers may not directly touch the UI thread affinitized window
handles, you correctly opted to use Control.Invoke to marshal the delegate
to the UI thread.
What basically happens when you call Invoke is :
- Put a Thread Method Entry object in a Linked list ( a queue), this TME
contains things like caller, target HWND, method delegate, arguments, the
compressed caller stack etc.
- Post a private USER Windows message (calling Win32 PostMessage) to the UI
thread application message queue.
- Wait for an Event that signals the end of the delegate method. (This is
what differentiates BeginInvoke from Invoke, in that BeginInvoke doesn't
wait but returns immediately)

When the UI thread message loop retrieves the private USER message from it's
application "message queue" in it's message handler, it also pulls the TME
from the linked list and executes the TME's method(function , eventhandler
etc..) on it's own thread (UI) using the arguments supplied and the
compressed caller stack. Whenever the method returns, the message handler
set's an Event signaling the caller that a method ran to completion. and
continues handling next application message.

If there's no further message in the UI thread's application message queue
(no further Posted messages), the Windows system will inspect the "input
message" queue and switch the message loop to this queue if there are input
messages (mouse, keyboard), however, as soon as there are new messages in
the application queue Windows will switch back to this queue for its message
processing. [note that not all input messages are placed in the input
queue!!]
What this all means is that Windows queues are prioritized by windows, that
means that posted messages have higher priority than input messages, and as
long as there are posted messages, no input messages (mouse clicks f.i) are
getting process. Worse, input messages are processed one at a time, that
means that a key-down event can be handled before a bunch of posted messages
followed by the handling of the key-up event.
To you it means that you should allow the message queue to drain at regular
intervals in order to allow input messages to be processed.
Now I still wonder why someone is placing all this stuff on the screen,
while I see hardly someone reading this at this speed.

Willy.
"Brian Keating EI9FXB" <csharp at briankeating.ne t> wrote in message
news:BC******** *************** ***********@mic rosoft.com...
Would I be correct in saying that the only way to get a user message into
a
Windows form would be to use P/Invoke with [Send/Post]Message?

Of is there some part of the .NET API that I am totally un aware of?

Thanks for any help
Brian

Nov 16 '05 #4
Hi Willy,

I'd a look at the messages coming through the WndProc this morning and
noticed this user message in response to the Invoke.
Your explanation has cleared up what exactly happens in response to a
..Invokexxx,
Also you've really opened my eyes with the "Input quque" this explains why
my input message are not getting handled.

Also at the end of your post you wonder about how quick someone can read,
you are quite correct; but consider an applicationt that writes quite alot to
the screen for one minute then goes off and idles for a while, the user can
scroll back along the messages that were outputed to the screen to see what
exactly happened.

However I think I know what you are getting at, and as also suggested by Ian
Griffiths, it makes alot more sence to update the UI at a refresh interval,
and display any new messages.

Thanks for your reply, I'm starting to see things alot more clearly.
best regards
Brian.

"Willy Denoyette [MVP]" wrote:
Seems like you are not completely clear how windows messages are handled in
Windows (Win32) and as such Windows.Forms.

Consider the example you posted recently:
In this you have:
- a main thread handling the UI and,
- two worker threads (called workers), handling external communication and
dispatching messages (Text) to the UI thread.

Because the workers may not directly touch the UI thread affinitized window
handles, you correctly opted to use Control.Invoke to marshal the delegate
to the UI thread.
What basically happens when you call Invoke is :
- Put a Thread Method Entry object in a Linked list ( a queue), this TME
contains things like caller, target HWND, method delegate, arguments, the
compressed caller stack etc.
- Post a private USER Windows message (calling Win32 PostMessage) to the UI
thread application message queue.
- Wait for an Event that signals the end of the delegate method. (This is
what differentiates BeginInvoke from Invoke, in that BeginInvoke doesn't
wait but returns immediately)

When the UI thread message loop retrieves the private USER message from it's
application "message queue" in it's message handler, it also pulls the TME
from the linked list and executes the TME's method(function , eventhandler
etc..) on it's own thread (UI) using the arguments supplied and the
compressed caller stack. Whenever the method returns, the message handler
set's an Event signaling the caller that a method ran to completion. and
continues handling next application message.

If there's no further message in the UI thread's application message queue
(no further Posted messages), the Windows system will inspect the "input
message" queue and switch the message loop to this queue if there are input
messages (mouse, keyboard), however, as soon as there are new messages in
the application queue Windows will switch back to this queue for its message
processing. [note that not all input messages are placed in the input
queue!!]
What this all means is that Windows queues are prioritized by windows, that
means that posted messages have higher priority than input messages, and as
long as there are posted messages, no input messages (mouse clicks f.i) are
getting process. Worse, input messages are processed one at a time, that
means that a key-down event can be handled before a bunch of posted messages
followed by the handling of the key-up event.
To you it means that you should allow the message queue to drain at regular
intervals in order to allow input messages to be processed.
Now I still wonder why someone is placing all this stuff on the screen,
while I see hardly someone reading this at this speed.

Willy.
"Brian Keating EI9FXB" <csharp at briankeating.ne t> wrote in message
news:BC******** *************** ***********@mic rosoft.com...
Would I be correct in saying that the only way to get a user message into
a
Windows form would be to use P/Invoke with [Send/Post]Message?

Of is there some part of the .NET API that I am totally un aware of?

Thanks for any help
Brian


Nov 16 '05 #5
> Would I be correct in saying that the only way to get a user message into a
Windows form would be to use P/Invoke with [Send/Post]Message?
Strictly, yes. Conceptually, no.
What I mean by "conceptual ly, no" is, if the thing emitting the 'message' is
part of the same .NET program as the windows form but in a different thread,
then the best way of getting a 'message' to the form is not to use an
*actual* message, but to use BeginInvoke, with a delegate, as I believe
others have pointed out. This is the recommended way of posting a 'message'
from a worker thread back to the user interface, and there will be lots of
examples of it, should you need them.
What I mean by "strictly, yes" is that if you're defining 'message' as an
*actual windows message*, i.e. WM_*, and the communication *must* be by this
means, then AFAIK the only way to get it to the form is to use PostMessage or
SendMessage.

Of is there some part of the .NET API that I am totally un aware of?
If you want to process custom messages, look into the following override,
that a form has:
public override bool PreProcessMessa ge(ref Message msg)

Thanks for any help
Brian

Nov 16 '05 #6
> the message basically is a string for the window to display,
I want to use the loop because in want to implement a prioritised message
loop, i.e., handle any windows message first then handle my user message.
If you really need this to happen, then I believe the only option will be to
rewrite the form in C, with a custom message pump. All windows have a message
pump that is basically similar to the following:

1 [start of loop] ) get the next message (using GetMessage) from the
operating system's queue for the application. If there aren't any messages,
the call blocks until there is one. If the message is WM_QUIT, quit the loop
and go to step 5.
2) "Translate" the message (using TranslateMessag e), this 'translates' the
message (what it actually does I don't know, just know that you have to do
it). Also accelerator keys are translated into messages in this step.
3) Dispatch the message to the WndProc of the window it was intended for
(using DispatchMessage )
4) Go back to (1), the start of the loop
5) Return the wParam of the message as the return value of the function that
invoked the window

What this means essentially, is that a window's wndproc only gets messages
in sequence, it doesn't "prioritise " messages in any way or let you decide
which get processed first. The wndproc has no concept of what the next
message will be and no option of doing, either (or what the previous one was
come to that, unless it explicitly takes steps to remember it).
The bottom line is that if you want to prioritise messages, you will have to
cook up your own custom message pump, probably using PeekMessage and an array
of messages, but whether that's advisable / necessary, I wouldn't know.
It theoretically could be done - if you peek into the queue and there's one
of your custom messages there, then it stores it in an array until there
aren't any messages waiting (PeekMessage returns zero). If there *are* more
messages waiting, then you would Get them, and if they are your custom one,
store them in the array of waiting ones, and if they're not, then just
dispatch them as normal. When there's no more waiting, you dispatch any that
are in the array.
The only downside is that you *can't* do this in .NET!
You can subclass to take control of processing of messages, and replace the
wndproc's default handling of the message with your own, but you can't do
this with the message pump - it's too low-level, the fundamental basis of the
operation of the window. You've got to get your hands dirty and delve into
raw hardcore C.

Reason: My user messages will be coming v.fast, So I want to enforce
processing of any windows messages before processing my user message.

thanks
Brian

"Patrik Löwendahl [C# MVP]" wrote:
Depends on what you want the message to do and why you want it in the loop?

--
Patrik Löwendahl [C# MVP]
cshrp.net - 'Elegant code by witty programmers'
cornerstone.se 'IT Training for professionals'

Brian Keating EI9FXB wrote:
Would I be correct in saying that the only way to get a user message into a
Windows form would be to use P/Invoke with [Send/Post]Message?

Of is there some part of the .NET API that I am totally un aware of?

Thanks for any help
Brian

Nov 16 '05 #7
> Now I still wonder why someone is placing all this stuff on the screen,
while I see hardly someone reading this at this speed.
I can think of several reasons:
a) programs that constantly spool a load of text, fast, look *cool*.
b) if it constantly spools a load of text there can be no doubt that it's
doing stuff.
c) you can read it faster if you know what it's likely to say. i.e, you
don't actually have to read it, so much as 'recognize' general textual
patterns.
d) if management walk over and see your screen while its running, it tends
to look like you've automated something off your own bat to happen really
fast that you would otherwise be doing manually slowly.


Willy.
"Brian Keating EI9FXB" <csharp at briankeating.ne t> wrote in message
news:BC******** *************** ***********@mic rosoft.com...
Would I be correct in saying that the only way to get a user message into
a
Windows form would be to use P/Invoke with [Send/Post]Message?

Of is there some part of the .NET API that I am totally un aware of?

Thanks for any help
Brian


Nov 16 '05 #8
> a) programs that constantly spool a load of text, fast, look *cool*.

This is true. However, you don't need to post hundreds of messages across
threads every second to make this happen.

In fact it's better if you don't.

You tend to get better results if you have as few transitions between the
worker thread and the UI thread as possible. If you throttle it to, say, 10
a second, and batch your updates, you can still scroll vast amounts of text
past the user, but you'll get your actual work done a lot faster.
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"Bonj" <Bo**@discussio ns.microsoft.co m> wrote in message
news:39******** *************** ***********@mic rosoft.com...
Now I still wonder why someone is placing all this stuff on the screen,
while I see hardly someone reading this at this speed.


I can think of several reasons:
a) programs that constantly spool a load of text, fast, look *cool*.
b) if it constantly spools a load of text there can be no doubt that it's
doing stuff.
c) you can read it faster if you know what it's likely to say. i.e, you
don't actually have to read it, so much as 'recognize' general textual
patterns.
d) if management walk over and see your screen while its running, it tends
to look like you've automated something off your own bat to happen really
fast that you would otherwise be doing manually slowly.


Willy.
"Brian Keating EI9FXB" <csharp at briankeating.ne t> wrote in message
news:BC******** *************** ***********@mic rosoft.com...
> Would I be correct in saying that the only way to get a user message
> into
> a
> Windows form would be to use P/Invoke with [Send/Post]Message?
>
> Of is there some part of the .NET API that I am totally un aware of?
>
> Thanks for any help
> Brian


Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
17574
by: Jon Hyland | last post by:
Ok, I'm a little rusty on this, it should be a simple problem but I can't figure it out. How can I handle form events in my main code page?? I'm creating a Windows App in C#. Rather than make my main form the startup object, I'd rather put my Main() function in a class or code file. Why? Because thats what I always do in standard C++ or VB. In my opinion, writing all my application logic in the form itself is sloppy and only for...
5
11361
by: Dave | last post by:
How do I check in a Windows Forms app if any controls have changed? I have a form that collects data, and I want to prompt the user if they try to exit the app, or load a new file, without saving changes that have been made. In MFC/Win32, you'd trap the WM_COMMAND/EN_CHANGE notification messages, etc. But, this doesn't seem to happen in Windows Forms. I tried Spy-ing a windows forms app, and the WM_COMMAND messages don't even get sent...
3
7406
by: Brian Keating EI9FXB | last post by:
Hello again, I've already placed a few posts on this topic. This time i've a simple application that exhibits my problem, I've placed sample solution 8k on my website should anyone be interested in having a look. http://briankeating.net/transfer/test.zip To recap the problem I expected (and found). I've a main GUI thead (main form), this GUI thread has an UpdateTextBox function that appends a string in a textbox and It also has a button...
6
2640
by: billr | last post by:
I have developed a small API for taking care of a lot of boiler plate stuff in a multi formed windows application, for example setting up a messaging thread framework. New Forms, in the appllication using the API, are subclassed to a Form contained within the API, and they are controlled (controlled in this instance means, kept alive, displayed and hidden) at runtime by a thread whose responsibility is this sole task. In order to run...
14
4109
by: | last post by:
Hi All, I am little confused here, hope you can help me. While processing WM_POWERBROADCAST (wParam=PBT_APMQUERYSUSPEND), I MUST to do some lengthy operation(30 sec) before system Suspends or hibernates. To achieve this, in my message handler when processing PBT_APMQUERYSUSPEND, I create another thread which takes care of making this lengthy operation, and when its done I
3
2645
by: Neal | last post by:
managed C++ VS 2003 I have a beginner question about windows forms.... I need to call a function when a certain limit has been reached, now with the way VS sets up the .NET windows Form I get confused. When I was using Directx everything was being run from a while loop, so that was no problem for me in seeing where to place conditional statements and other functions. With windows forms do I need to have an event and eventhandler? it...
3
3732
by: RBarryYoung | last post by:
How can I get the following two features in the same program in VS2005?: 1) Access to the command-line arguments (cmdArgs()) that started my App. 2) Shutdown Mode = "When last Form exits". The first feature requires a "Sub Main(cmdArgs())", however my app exits when the Main Sub exits, EVEN if I have an open & running Form.
28
7377
by: | last post by:
I have a multi threaded windows form application that runs great after calling Application.Run(). Application.Run is required for a COM component I a using in the app (required for message loop). I have created a windows service from VStudio 2005 template. What is the windows service replacement for Application.Run()?
21
3377
by: Dan Tallent | last post by:
In my application I have a form (Customer) that I want to be able to open multiple copies at once. Within this form I have other forms that can be opened. Example: ZipCode. When the user enters a zipcode that is unknown this form will open. I don't want users to modify any of this customers data until they close the zipcode form. Normally this can accomplished using a modal form, however this prevents me from opening a new copy of...
0
8994
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9250
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8247
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4878
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.