13

I occasionally need to process a large amount of data from one package off the network, which takes sufficiently long that when the user tries to interact with the application windows adds the "(Not Responding)" string to the window title. I am aware this is because the processing is being done within a call to handle a message (some way up the stack) and therefore is blocking the message pump. I'm also aware the ideal way to deal with this is to process the data asynchronously in a separate thread so the pump can continue running, however this is a LARGE desktop application which is single threaded from top to toe and safely spinning this processing off is not feasible in our time frame.

So with that in mind, is there by any chance a way I can at least avoid the "not responding" moniker (which to most users reads as "has crashed") by telling windows my application is about to be busy before I begin the work? I believe there is something along these lines when responding to a request to close, one can keep asking windows for more time to avoid it proclaiming that your not "closing in a timely fashion"

I should add this is a C++ MFC application.

Gareth
  • 133,157
  • 36
  • 148
  • 157
Foo42
  • 3,024
  • 4
  • 22
  • 17
  • 2
    Both Windows and your users are correct. Your app IS not responding, and from the users viewpoint, it might very well have crashed. Don't fight the symptoms, fix the cause! – MSalters Jan 02 '09 at 14:15
  • Related: http://stackoverflow.com/questions/6997310/busy-application-leads-to-false-not-responding-state-on-windows-7-wm-update. – DuckMaestro Jul 23 '15 at 23:48

11 Answers11

17

I don't think the Windows API can help you here.

Alternatively, how about showing a dialog box with a progress bar and make it run in a separate thread?

A text like "This operation may take half an hour" on the dialog box may be appropriate too.

G S
  • 35,511
  • 22
  • 84
  • 118
11

Ok, firstly I upvoted Frederick's post because like it or not, the second thread is probably the best way to go.

However, if you really don't want to go down this road, you could manually pump the message queue within your apps inner loop. Something like this;

int Refresh()
{
    MSG       msg;
    if (PeekMessage (&msg, NULL, 0, 0,PM_NOREMOVE))
        if ((msg.message == WM_QUIT) 
          ||(msg.message == WM_CLOSE) 
          ||(msg.message == WM_DESTROY) 
          ||(msg.message == WM_NCDESTROY)
          ||(msg.message == WM_HSCROLL)
          ||(msg.message == WM_VSCROLL)
          ) 
          return(1); 
    if (PeekMessage (&msg, NULL, 0, 0,PM_REMOVE))
    {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    }
    return(0);
}

This is actually a piece of code I used prior to rewriting something similar as a seperate thread. Basically I have a look at the queue, filter out unwanted messages, and post on the rest. It works to an extent, but caused some occasional nasty side effects, hence the rewrite.

SmacL
  • 22,555
  • 12
  • 95
  • 149
9

You don't have to actually do anything with the messages from PeekMessage. Just call PeekMessage, you don't even have to remove anything from the queue or process it. As long as it is called every 5 seconds or so, it will cause windows to think the process is still responsive.

An alternative idea is to have a separate process/thread that will appear in the notification tray and inform the user that the process is busy waiting for an internal operation to complete. You'll see these in the later versions of Visual Studio, SQL Server Management Studio, etc.

adzm
  • 4,148
  • 1
  • 27
  • 21
8

Win32 has a method for this in user32.dll.

DisableProcessWindowsGhosting()

Disables the window ghosting feature for the calling GUI process. Window ghosting is a Windows Manager feature that lets the user minimize, move, or close the main window of an application that is not responding.

In addition to the above documented behavior, I also verified here (in a C# application) that this Win32 call also prevents the Not Responding label from appearing on the window as desired.

I found this via the C# answer to similar question here: https://stackoverflow.com/a/15380821/29152.

RED SOFT ADAIR
  • 12,032
  • 10
  • 54
  • 92
DuckMaestro
  • 15,232
  • 11
  • 67
  • 85
  • This won't prevent the users from realizing that the program's UI isn't responding; it actually just makes things worse: if you call that function, the users will still realize that the window isn't responding, but they won't be able to move it or minimize it like they could if it were ghosted. – SamB Aug 25 '15 at 19:44
  • 2
    @SamB it may be worse UX, but it's still a direct answer to what the OP is looking for. I had to research this myself for a different, more legitimate reason, which is for a full-screen game that Windows thought was not responding -- full-screen app was performing a long loading, already providing "loading..." feedback to the user. Undesirable for Windows to do anything more in this case. – DuckMaestro Aug 25 '15 at 20:34
5

If you fork off a thread you're most likely worried about some other user action happening which may depend on the result of the long running operation (yeah, concurrency). So expanding on what Fredrick said, if you do spin off a new thread and put up a progress bar, you could lock the focus onto the progress bar to stop a user from interacting with the rest of the application. That should be enough to implement a really simple second thread without really having to worry about concurrency because you're essentially locking out the rest of the app by disabling user interation.

user49913
  • 3,018
  • 1
  • 17
  • 8
4

You'll need to interleave the processing with message handling somehow. If threads are out of the question, you might want to look at splitting the processing into multiple phases. One way to do this is to do some processing when you first receive the packet, then post a message to the application saying "continue processing here". When the application receives the "continue processing here" message, it will do some more processing, and either send another "continue processing here" message or finish up.

There are a couple of considerations though:

  1. You need to make sure that the state of the application is consistent every time you post a message to yourself and defer to the message loop, as other message handling might happen in the mean-time. This can be done e.g. by only changing the state in the final processing phase.
  2. Another packet might arrive while you are still processing the first packet. If changing the order of processing would be bad for the application, you could handle this by e.g. posting a "remind me to process this packet later" message when this happens.

I don't know whether this would be feasible within the design of your application, but it would be one way to solve the problem.

Rune
  • 404
  • 2
  • 5
2

If you are unwilling to spawn a worker thread, but you can break the long-running task down into smaller parts, you can do the processing in MFC's CWinApp::OnIdle. This function gets called from within the message pump loop whenever there are no Windows messages waiting. As long as the work you do in each OnIdle call is sufficiently short, you keep your app responsive.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

Assuming that it is the processing of the data that is taking up all the time and not the receiving (and you're serious about avoiding a thread - which is fine IMOHO) of the data you could:

  1. In the function that you are currently handling the message, create a modal dialog that shows a "please wait" message (or make it hidden, small, whatever...). Copy (or send a pointer, etc...) the data you're processing to a member variable of that dialog.
  2. In the modal dialog post a user-defined message to yourself to process the data.
  3. In the dialog's message handler, handle one "unit" of work. Keep track what the next "unit" of work is. Post the same message again.
  4. Repeat this post-message "loop" until done. Close your dialog.

The nature of the modal dialog will keep you're application "responsive", with minimal interruption or change to how the application worked previously. Reentrancy can be a problem with modal loops, especially if any of this is involved with a WM_PAINT message. (anyone ever assert inside painting code? good times, good times...)

The dialog could even have a cancel button if you'd like.

Aardvark
  • 8,474
  • 7
  • 46
  • 64
0

I encountered the exact same problem. Since I dont consider the other answers appealing/straightforward I decided to post this.

Short description and some context: I am saving data from a grid into a database, and this process can take a while. So I changed the saving method to an asynchronous method and had the same problem.

Then I came up with a simple solution:

//__ENABLE OR DISABLE MAIN DIALOG
void CMFCApplication1Dlg::enableMainDlg(bool enable)
{ 
    this->EnableWindow(enable);
}

When starting the asynchronous method, I disable the main dialog. This prevents the user from interacting with the main dialog (like starting another saving process which could result in thousands of SQL error messages if I wouldn't check if the saving process is already running...)

When the saving process is finished, I re-enable the main dialog. Works like a charm, I hope this helps

KBell
  • 3
  • 1
0

One way to overcome your application from becoming unresponsive you need to tell the application to process messages from windows. When you are in your loop you can call

Application->ProcessMessages();
Marcello B.
  • 4,177
  • 11
  • 45
  • 65
0

I had a similar issue with a win32 app that was waiting on a response from webservice using cpprest (Casablanca) api. My solution was to create a event and thread that does nothing but wait for the cpprest api and then release the thread once it recieves the signal:

DWORD WINAPI WaitForCasablanca(LPVOID n)
{

// Get the handler to the event for which we need to wait in 
//    this thread.
 HANDLE hEvent = OpenEvent(EVENT_ALL_ACCESS, false, "MyEvent");
if (!hEvent) { return -1; }
// Loop through and wait for an event to occur

    // Wait for the Event
    WaitForSingleObject(hEvent, INFINITE);
    //    No need to Reset the event as its become non signaled as soon as
    //    some thread catches the event.

CloseHandle(hEvent);

return 0;}

BOOL WINAPI DlgProc(HWND hDlg, UINT message, WPARAM,wParam, LPARAM lParam) ...
  HANDLE     hEvent = CreateEvent(NULL, false, false,        "MyEvent");//create an event that will wait for casablanca ro authenticate
if (!hEvent) return -1;
            //    Create a Thread Which will wait for the events to occur
  DWORD Id;
  HANDLE hThrd = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WaitForCasablanca, 0, 0, &Id);
            if (!hThrd) { CloseHandle(hEvent); return -1; }
   makeCasablancaRequest(...);
 SetEvent(hEvent); //casablanca has finished signal the event to terminate

    WaitForSingleObject(hThrd, INFINITE); //wait for thread to die

            CloseHandle(hThrd);
            CloseHandle(hEvent);
            ...}

That got rid of the "program not responding" message for me. I believe the problem is the code that is getting the data is running in a thread too- only the main program doesn't know this- so as far as the system is concerned the main program is idling. You need an event and a thread that waits for the event to tell the system the program is waiting on data. I got the code from this tutorial: How to use WIN32 Event Kernel Object

IjonTichy
  • 96
  • 1
  • 1
  • 10