Recent

Author Topic: How can I figure out what is preventing my application from closing?  (Read 3660 times)

fatmonk

  • Sr. Member
  • ****
  • Posts: 252
My apologies for such a vague an open ended question. I do realise that the reason could be almost anything, but that's the problem and I need some general advice on how to debug the issue.

I'm making really goo progress with an application that uses ASyncCalls and MouseInput.Click() to programatically do mouse clicks for the user.

Pretty much everything is working as required now with all clicking variations needed working.

However, when the application does a left click over the 'Close' button of the main form - to exit the application - the application hangs until the mouse is manually clocked again. The application starts running again as soon as the mouse button is manually clicked (I can see this because the UI also has a mouse coordinated tracker in the title bar). If this manual click is on the Close button the form DOES close.

I can't work out what could be causing this unless it's something to do with the ASyncCall to MouseInput.Click() not having finished (even thought the click appears to have happened before the hang).

And I don't know where to start looking.

I've attached a demo of the problem in a simplified project - this project pulls together a few of the things I've been working on in the actual application, including the transparent underlay that can be enabled using the checkbox.

In the demo application left-click, right-click etc work as expected anywhere on the screen. Except that left-click on the close button of the application itself does not close the app, but freezes it.

Thanks,

FM

balazsszekely

  • Guest
Re: How can I figure out what is preventing my application from closing?
« Reply #1 on: August 23, 2016, 12:52:33 pm »
You most likely deadlock the application. Inside aSyncMouseClick procedure, check if the mouse is not over the titlebar. If it is, don't use MouseInput.Click which internally uses SendInput Api under windows, go instead with the Mouse_Event api(it will work 100%).

balazsszekely

  • Guest
Re: How can I figure out what is preventing my application from closing?
« Reply #2 on: August 23, 2016, 01:16:30 pm »
Just to prove that it works:
Code: Pascal  [Select][+][-]
  1. uses windows;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. begin
  5.   //move cursor over the close button(you have five seconds)
  6.   Timer1.Interval := 5000;
  7.   Timer1.Enabled := True;
  8. end;
  9.  
  10. procedure TForm1.Timer1Timer(Sender: TObject);
  11. var
  12.   P: TPoint;
  13. begin
  14.   Timer1.Enabled := False;
  15.   if GetCursorPos(P) then
  16.   begin
  17.     Mouse_Event(MOUSEEVENTF_ABSOLUTE OR MOUSEEVENTF_LEFTDOWN, P.X, P.Y, 0, 0);
  18.     Sleep(40);
  19.     Mouse_Event(MOUSEEVENTF_ABSOLUTE OR MOUSEEVENTF_LEFTUP, P.X, P.Y, 0, 0);
  20.   end;
  21. end;

fatmonk

  • Sr. Member
  • ****
  • Posts: 252
Re: How can I figure out what is preventing my application from closing?
« Reply #3 on: August 23, 2016, 01:29:56 pm »
It does!

And your example answers one of the questions that I was about to ask as I was getting confused by the MSDN documentation on Mouse_Event.

Now.. is there any reason not to just use Mouse_Event instead of Mouse_input anyway?

I'm guessing Mouse_Event could be Windows specific as it's documented on MSDN - is that the case?

Finally, is there an easier way to determine whether the mouse is over the title bar than comparing the mouse positions with Top and Left of the form and GetSystemMetrics(SM_CYCAPTION);?

-FM

balazsszekely

  • Guest
Re: How can I figure out what is preventing my application from closing?
« Reply #4 on: August 23, 2016, 01:47:21 pm »
Quote
Now.. is there any reason not to just use Mouse_Event instead of Mouse_input anyway?
SendInput is a more generic api, it can also handle keyinput for example. It's really up to you which one you choose. In this situation I would go with Mouse_Event, because it works better then SendInput.

Quote
I'm guessing Mouse_Event could be Windows specific as it's documented on MSDN - is that the case?
Yes it is! SendInput is also windows specific. You can check the linux/osx equivalents in the lazmouseandkeyboard package.

Quote
Finally, is there an easier way to determine whether the mouse is over the title bar than comparing the mouse positions with Top and Left of the form and GetSystemMetrics(SM_CYCAPTION);?
No, not really. You can catch the WM_NCMOUSEMOVE message and check the HTCAPTION param. Catching messages under lazarus it's not that simple though.

fatmonk

  • Sr. Member
  • ****
  • Posts: 252
Re: How can I figure out what is preventing my application from closing?
« Reply #5 on: August 23, 2016, 02:04:56 pm »
It looks like Mouse_Event doesn't support ShiftStates though so I can't use it as a complete replacement...

Will work for simple left clicks, though, so with a bit of thought I can make this work.

Thanks,

FM

 

TinyPortal © 2005-2018