"Amjad" <Am***@discussions.microsoft.com> schrieb
I want to communicate with an RS232 COM port, so I transmit a
command to the port and listen for replies in a threaded infinite
loop. In my WindowsForm application I used the
"Application.Doevents" after sending the command in the loop. I also
used it in another function after calling a Dial-up "Connect"
method.
It seemed to me that the "Doevents" method was important to force
Microsoft Windows (and not necessarily the WindowsForm) to process
any messages generated by my application now.
I'm concluding from your replies that a DLL module cannot generate
Windows Messages.
It doesn't matter if the code is an a module or in a (not-module) class.
Whether you can use DoEvents or not - like the others have already said -
depends on the fact whether the code is running in a UI (user interface)
thread. A UI thread is a thread that has a message queue. A thread gets a
message queue if you create a window (AKA Control, Form) in the thread. The
message loop is there to process all the messages and it runs all the time
to wait for the messages. In .Net, the message loop is contained in
Application.Run. If you write your own sub main, you have to call
Application.Run to keep the thread/app alife. If you specify a Form as the
startup object, VB.Net internally creates a sub main that only contains
"application.run(new Startupform)".
'DoEvents' is a kind of 'workaround' if you are running in a loop where the
thread's main task is doing a job while still being able to process the
messges in the message queue. DoEvents also processes theses messages.
Usually, the job that I mentioned should then be pushed to a different
thread so that the main thread, which is the UI thread, can keep on
processing messages while the other thread is doing it's job without the
need for DoEvents.
In a service, there is no UI. Thus, you don't have a message loop. Long
story short: The question is whether you would need a message loop to get
the events from your COM port. If possible, do the following:
1. make a test application (WindowsApplication!)
2. Write an event handler for the event that you need to handle
3. set a breakpoint in the event handler
4. run
5. Post the full callstack here
Depending on the component you are using, it might be possible that it does
*not* depend on windows messages because some components offer a callback
mechanism instead. If this is there, use it. Otherwise, and if a message
loop is not allowed in a service (I am not a service expert), you can not
use the component in the service.
Armin