My form receives asynchronous callbacks from another object on random worker threads. I have been passing the data to the main thread (where it can be used to update onscreen controls) using delegates as shown below. Performance is dreadful — once I reach 500 updates per second, the program completely locks up. My GUI processing itself is not the problem, as I can simulate this level of updating within the form and have no problems. Is there a more efficient mechanism I should be using to hand off the data from thread to thread?
delegate void DStatus( MyStatus obj ); DStatus _status; // set to MainThreadOnStatus during construction // this function only called on form's owner thread void MainThreadOnStatus( MyStatus obj ) { // screen updates here as needed } // this function called by arbitrary worker threads in external facility void OnStatus( MyStatus obj ) { this.BeginInvoke( _status, obj ); }
You probably don’t need to update UI on every event, but rather ‘not as often as X times per second’. You may utilize StopWatch or other timing system to collect events during a period of time, and then update UI when appropriate.
If you need to capture all events, collect them in the Queue and fire event every so often, and that event handler will process the Queue and update UI once for all queued events.