I am working on Win.Forms data-bound applications, where I discovered strange behavior. The application has separate I / O streams that receive updates via asynchronous web requests, which are then sent to the main / graphical interface stream for processing and updating application data stores (which, in turn, can be bound to various GUI elements, etc. .). The server at the other end of the web request requires periodic requests or session time.
I reviewed several attempts to solve thread problems, etc., and I noticed the following behavior:
If I use Control.Invoke to send updates from I / O streams to the main stream, and this update causes the MessageBox to appear, the main form message pump stops until the user clicks the ok button. It also blocks the continuation of the I / O stream, which ultimately leads to timeouts on the server.
If I use Control.BeginInvoke to send updates from input / output streams to the main stream, the main message flow of the message does not stop, but if the processing of the update leads to the display of the message window, the processing of the rest of this update is suspended until the user types OK . As I / O streams continue to work and message processing is retained on the message pump, some BeginInvoke calls may be called before the message with the message field ends. This results in invalid out-of-line updates.
I / O-threads add updates to the lock queue (very similar to Creating Queue <T> Lock in .NET? ). The GUI-thread uses Forms.Timer, which periodically applies all updates to the lock queue. This solution solves both the problem of blocking I / O streams and the sequence of updates, that is, the next update will never be launched until the previous one is completed. Nevertheless, there is a small cost of execution, as well as the appearance of a delay in showing updates that are unacceptable in the long term. I would like for updates to be processed in the main thread using events rather than polling.
So to my question. How can I do it:
- avoid blocking input / output streams
- ensure that updates are completed in sequence
- keep the main message pump on, showing the message box as a result of the update.
Update: see solution below
source share