Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I close an app without a modal dialog box being shown?

I have an app that can optionally open PDF's after it creates them. If two reports are generated in succession with the same name, the second attempt fails if the first copy of acrobat still has the PDF open, so before I write the PDF I check (with FindWindow) for a window with the document name. If one is found I issue a SendMessage WM_Close.

This works OK, but I was doing some other tests and was using Word to "edit" the PDF, to hold it open so I could test the app's behaviour when it can't write the PDF file. Now, when my app tries to close the window, Word pops up a "do you want to save" dialog. If I click cancel, Word remains open, my app carries on and I can test that it behaves sensibly when it encounters a file that it can't write to.

All good, but it has alerted me to the fact that using SendMessage WM_CLOSE to close another app will snag my app if the other app pops up a modal dialog. Is there any way around this - i.e a more forceful (but not too forceful) way of closing the other app? Or a "Close and click on cancel if necessary". Or should I use asynchronous messages?

like image 374
rossmcm Avatar asked Dec 12 '22 07:12

rossmcm


1 Answers

Do not force any application to close, there may be other documents open the user is viewing etc... You can use SendMessageTimeout to wait the return of WM_CLOSE a sensible amount of time, and then proceed with either failure or success..

var
  Word: HWND;
  msgResult: DWORD;
begin
  ...

  SendMessageTimeout(Word, WM_CLOSE, 0, 0, SMTO_NORMAL, 5000, msgResult);
  if IsWindow(Word) then begin
    // bummer! Application is open...
 
like image 185
Sertac Akyuz Avatar answered May 12 '23 23:05

Sertac Akyuz