0

I got the below code, which I believe producing "Resource is busy" and since it is in a Timer keep on repeating. I use it to find out what the website I am trying to automate telling me and act accordingly.

Anyone can tell me how to fix the code or debug it?

    procedure TimerAutoOKTimer(Sender: TObject);
     const
          Messagefromwebpage = 'Message from webpage';

       var
          wnd: hWnd;

    begin
          wnd := FindWindow(nil, Messagefromwebpage);  // 'Message from webpage';
          if wnd <> 0 then
          begin
             Memo1.Lines.Clear;
             EnumChildWindows(wnd, @EnumChildrenProc, Integer(Memo1.Lines));

             if POS('NOT possible', Memo1.Lines.Text)>0 then
               SendMessage(Application.ActiveFormHandle, WM_COMMAND, ID_OK, 0);

             if Pos('The webpage you are viewing is trying to close the window.', Memo1.Lines.Text)>0 then
               SendMessage(Application.ActiveFormHandle, WM_COMMAND, ID_NO, 0);
          end;
    end;

screenshot of the error

modzsi
  • 185
  • 2
  • 12
  • So, what is producing "Resource is busy"? – David Heffernan May 15 '13 at 12:08
  • Not sure I suspect it is either SendMessage or EnumChildWindows? – modzsi May 15 '13 at 12:16
  • Well, which one is it. Comment out the `SendMessage` calls. I bet it is them. My guess is that you need to use `PostMessage` instead. – David Heffernan May 15 '13 at 12:16
  • Where is the window that you are trying to automate? Is it in your process? – David Heffernan May 15 '13 at 12:29
  • It is raised by my process through WebBrowser. I tried swapping to PostMessage, same error. :( – modzsi May 15 '13 at 12:39
  • So what is `ActiveFormHandle`? Which window is that? – David Heffernan May 15 '13 at 12:42
  • It is a single Form app. I am using it since it is the only thing seemed to work when I send the message to the TWebBrowser Popup Window. I tried using "wnd" variable but for some reason it did not click... – modzsi May 15 '13 at 12:55
  • Well, sending those messages to random windows isn't going to get it done. – David Heffernan May 15 '13 at 12:57
  • I see. Must admit, it worked for a while :) 1. So basically I will need to set focus on the "wnd" and then click? 2. Application.ActiveFormHandle != the current form handle of my app? – modzsi May 15 '13 at 13:09
  • You're already enumerating the children of the dialog, you can try finding the OK button to send a BM_CLICK message. It may require setting focus and clicking the button a few many times; I once had some trouble clicking a button of a ie window (http://stackoverflow.com/a/13496333). – Sertac Akyuz May 15 '13 at 13:15
  • Are you sure it's not your timer firing too often? Set a form property FInTimer to prevent entering the routine again while it's busy. – Jan Doggen May 15 '13 at 13:26
  • I changed SendMessage to use wnd and for now it works fine :) I am testing now. @David, Sertac, Jan thanks for your help! – modzsi May 15 '13 at 13:44
  • @JanDoggen The timer cannot fire again because the message queue is not serviced during the timer proc – David Heffernan May 15 '13 at 14:15
  • I realised it works for most windows, but one which only differs in the text... Since it is not clicked OK, next time it will raise the error "resource is busy" – modzsi May 15 '13 at 14:38
  • @SertacAkyuz I tried your code, It clicks and then stucks in a loop :( ` SetForegroundWindow(wnd); bwnd := GetDlgItem(wnd, ID_OK); if bwnd <> 0 then begin // click the button // the dialog/button is kind of deaf.. while IsWindowEnabled(wnd) do begin SendMessage(bwnd, BM_CLICK, 0, 0); Sleep(100); end; wnd:=0; end; ` – modzsi May 15 '13 at 15:37
  • @mod - Sure you've got a find a way out of the loop. Testing if the dialog is enabled was OK for that question, your situation is different. My only point was, ie dialogs might not register a click for a few times. – Sertac Akyuz May 15 '13 at 15:44
  • Managed to figure but still on of the windows not getting my clicks: `ID: 2, ClassName: Button, Caption: OKID: 20, ClassName: Static, Caption: ID: 65535, ClassName: Static, Caption: Unable to Archive Product...` THis is one work though: `ID: 1, ClassName: Button, Caption: OKID: 2, ClassName: Button, Caption: CancelID: 20, ClassName: Static, Caption: ID: 65535, ClassName: Static, Caption: Are you sure you want to delete this batch item?` – modzsi May 15 '13 at 16:29

0 Answers0