Help need with POSTMESSAGE function in C#

  • Thread starter Thread starter Srinivasa Ra via .NET 247
  • Start date Start date
S

Srinivasa Ra via .NET 247

(Type your message here)
I am writing an application that does lot of read/write's withcomputer's serial port. The application as a whole is workingfine.
Current Approach: I have a Timer that checks once every millisecond to see if there is any data avaialable to read at theserial port. If so then reads it. The serial port is openedwithout OVERLAPPING.

TRYING TO GET TO: I don't think polling serial port every millisecond is a good approach. I am thinking of creating a seperatethread that waits for data to come at the serial port. When thedata arrives, it should inform it to the main thread which ishandling the User Interface. For this, I am trying to use Win32API function POSTMESSAGE to post data to the main form window'smessage loop. I have overriden both PreProcessMessage() andWndProc() functions. But no use. I never get that message to themain thread. But no compilation errors. Am I doing somethingwrong here?? I don't want to use delegates, threadpool stuff.

Any suggestions would be highly appreciated.

Thank you all
 
Srinivasa,

Don't use the PostMessage routine. Rather, make a control from the UI
thread available to the worker thread. On the worker thread, you call
Invoke, passing a delegate, and the parameters that are to be passed to the
delegate.

Calling Invoke on the control with the delegate causes the delegate to
be invoked on the UI thread. So basically, you would write a function which
would update your UI, and then create a delegate that will wrap the function
and pass that to Invoke from your worker thread.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

(Type your message here)
I am writing an application that does lot of read/write's with computer's
serial port. The application as a whole is working fine.
Current Approach: I have a Timer that checks once every milli second to see
if there is any data avaialable to read at the serial port. If so then reads
it. The serial port is opened without OVERLAPPING.

TRYING TO GET TO: I don't think polling serial port every milli second is a
good approach. I am thinking of creating a seperate thread that waits for
data to come at the serial port. When the data arrives, it should inform it
to the main thread which is handling the User Interface. For this, I am
trying to use Win32 API function POSTMESSAGE to post data to the main form
window's message loop. I have overriden both PreProcessMessage() and
WndProc() functions. But no use. I never get that message to the main
thread. But no compilation errors. Am I doing something wrong here?? I don't
want to use delegates, threadpool stuff.

Any suggestions would be highly appreciated.

Thank you all
 
Back
Top