How to prevent a Not Responding message on a window's title bar?

13,329

Solution 1

You need to use a BackgroundWorker so that the time consuming task will run in a separate thread asynchronously. That will allow Windows multitasking to make the UI responsive. You should use a wait cursor or some other visual indicator to let the user know that your application is busy.

From MSDN MSDN BackgroundWorker

The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

To execute a time-consuming operation in the background, create a BackgroundWorker and listen for events that report the progress of your operation and signal when your operation is finished. You can create the BackgroundWorker programmatically or you can drag it onto your form from the Components tab of the Toolbox. If you create the BackgroundWorker in the Windows Forms Designer, it will appear in the Component Tray, and its properties will be displayed in the Properties window.

To set up for a background operation, add an event handler for the DoWork event. Call your time-consuming operation in this event handler. To start the operation, call RunWorkerAsync. To receive notifications of progress updates, handle the ProgressChanged event. To receive a notification when the operation is completed, handle the RunWorkerCompleted event.

Solution 2

You need to run your huge task on a background thread so as not to lock up the UI (main) thread.

Share:
13,329
shamim
Author by

shamim

Working on Microsoft .NET Technology with over 8 years of industry experience. Responsible for developing and managing several applications build on .NET technology. In my day to day, work needs to maintain a set of well-defined engineering practices developed by me and as well as other developer communities, having knowledge in domain-driven design, design architectural framework, user experience, usability engineering and project management.

Updated on June 09, 2022

Comments

  • shamim
    shamim about 2 years

    I use VS2010 and C# to build a desktop application. This application has one form with a huge task which takes a lot of time to complete. When this form is initialized it works perfectly, except it shows “Not Responding” on the title bar, like the picture shows:

    enter image description here

    After completing all tasks, it shows the desired output. Why is this message shown, and how do I prevent it?

  • shamim
    shamim about 12 years
    BlackSpy thanks for reply,don't understand this lines "lock up the UI (main) thread",would you please tell some in detail.
  • tsells
    tsells about 12 years
    I would suggest doing some research on the Backgroundworker class and running your processes in there. That will prevent the UI from freezing while waiting for your long running operation to complete. This is due to the "work" being run on a different thread than the UI so the UI events (paint, refresh, etc) are not blocked.