Application Delivery Management
Application Modernization & Connectivity
IT Operations Management
CyberRes
Problem:
From within a Windows Forms application, when a button is clicked the program would disable the button and then perform the task associated with the button from within the button Click event handler. If this is a long-running task then the UI thread can become unresponsive and start queuing events, even for those such as a mouse click on the disabled button. When the button is enabled again the queued events would be run and the event handler code would be invoked again in error.
Solution:
This demo shows one possible method of avoiding this scenario by executing a long-running task by creating a BackgroundWorker thread and running the task separately from the main UI thread. In this manner the UI thread is free to keep processing events and the BackgroundWorker thread can report its progress and also be canceled if need be.
The zipped up Visual Studio 2019 solution for BackgroundWorkerDemo is attached to this article.