Send or post a message to a Windows Forms message loop

前端 未结 3 433
离开以前
离开以前 2021-02-04 05:29

I have a thread that reads messages from a named pipe. It is a blocking read, which is why it\'s in its own thread. When this thread reads a message, I want it to notify the Win

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 06:00

    Are you actually wanting to post a message to the message loop or are you simply wanting to update some control in your Form, display a message box, etc.? If it's the former, then refer to @Noldorin's response. If it's the latter, then you need to use the Control.Invoke() method to marshal the call from your "reading" thread to the main UI thread. This is because controls can only be updated by the thread they were created on.

    This is a pretty standard thing in .NET. Refer to these MSDN articles to get the basics:

    • Control.Invoke Method
    • Control.InvokeRequired Property (refer to first example in Community Content)

    Once you understand how to do this, refer Peter Duniho's blog for how to improve on the canonical technique.

提交回复
热议问题