Recent

Author Topic: [SOLVED]App freezing/stalling when button on title bar is clicked  (Read 1440 times)

bobix

  • Jr. Member
  • **
  • Posts: 70
    • http://rechnik-bg.com
Can someone tell me why an application stops if button on the title bar of the form is left clicked and held clicked? I mean Minimize, Maximize and Close. Or how to avoid that behaivior.

The same application on Linux does not have such problem and nothing stops.

To recreate:
Run my example project.
Click on all 3 buttons.
Click on minimize with left mouse button and hold it. All stops. Move the mouse outside the button to avoid minimizing and see that nothing happened in that time.

Thank you!  :)
« Last Edit: August 09, 2018, 04:26:30 pm by bobix »
Lazarus 1.6.2 r53354 FPC 3.0.0 i386-win32-win32/win64

tetrastes

  • New member
  • *
  • Posts: 25
Re: App freezing/stalling when button on title bar is clicked
« Reply #1 on: August 09, 2018, 01:26:18 pm »
This is standard behavior for Windows. You may see it in any windows app with something changing, for example Task Manager.

bobix

  • Jr. Member
  • **
  • Posts: 70
    • http://rechnik-bg.com
Re: App freezing/stalling when button on title bar is clicked
« Reply #2 on: August 09, 2018, 04:26:08 pm »
Code: Pascal  [Select]
  1. uses
  2. windows;
  3.  
  4. var
  5.   PrevWndProc: WNDPROC;  
  6.  
  7. function WndCallback(HWnd: HWND; Msg: UINT; WParam: WParam; LParam: LParam):LRESULT; stdcall;
  8. begin
  9.   if Msg = WM_NCLBUTTONDOWN then //left down
  10.   begin
  11.     if WParam=8 then //minimize clicked
  12.      begin
  13.        Application.Minimize;
  14.      end;
  15.     if WParam=9 then //maximize clicked
  16.      begin
  17.       if form1.WindowState=wsNormal then form1.WindowState:=wsMaximized else form1.WindowState:=wsNormal;
  18.      end;
  19.    if WParam=20 then //close clicked
  20.     begin
  21.      form1.close;
  22.     end;
  23.    if WParam=3 then //Menu In Title Bar Application Icon
  24.     begin
  25.  
  26.     end;
  27.     exit;
  28.   end;
  29.   if Msg = WM_NCRBUTTONDOWN then //right down
  30.   begin
  31.     exit;
  32.   end;
  33.   Result := CallWindowProc(PrevWndProc, HWnd, Msg, WParam, LParam);
  34. end;
  35.  
  36. procedure TForm1.FormCreate(Sender: TObject);
  37. begin
  38.  PrevWndProc := Windows.WNDPROC(SetWindowLongPtr(Self.Handle, GWL_WNDPROC, PtrInt(@WndCallback)));
  39. end;
  40.  
Not perfect may be, but works for me :D
« Last Edit: August 09, 2018, 05:14:53 pm by bobix »
Lazarus 1.6.2 r53354 FPC 3.0.0 i386-win32-win32/win64

lucamar

  • Hero Member
  • *****
  • Posts: 514
Re: App freezing/stalling when button on title bar is clicked
« Reply #3 on: August 10, 2018, 03:37:21 am »
Not perfect may be, but works for me :D

"Not perfect", indeed. You're responding to "button down" messages instead of letting Windows process button clicks. It may not matter that much normally but introduces an element of surprise that will annoy users some times (and some users all the times). Best of luck to you   :P

What is happening here (simplyfied a lot) is that as soon as Windows detects a button-down event on the non-client area it stops sending update messages, gets a snapshot of the window and gets ready to start drawing all those nice effects for window-moving, -resizing, etc. The window then stays frozen until the corresponding mouse-up ends the impasse.

Nothing you can do about it, except--as shown by boblx--interfere with the process, which is not recomended.
« Last Edit: August 10, 2018, 03:45:18 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/FPC 3.0.4 on:
(K)Ubuntu 11..16, Windows XP SP3 (Home/Prof.) and various DOS incarnations.

bobix

  • Jr. Member
  • **
  • Posts: 70
    • http://rechnik-bg.com
Re: [SOLVED]App freezing/stalling when button on title bar is clicked
« Reply #4 on: August 10, 2018, 10:42:07 am »
I agree :)
There should be better way to avoid hanging the threads. Some developers found a way. Browsers does not freez, media players does not freez, torrents are keep getting downloaded while you are holding mouse button. But as you can see in my example, not just drawing, but everything stops.
So me and everyone else here is open for that better solution :D
Lazarus 1.6.2 r53354 FPC 3.0.0 i386-win32-win32/win64

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: [SOLVED]App freezing/stalling when button on title bar is clicked
« Reply #5 on: August 10, 2018, 10:49:03 am »
I agree :)
There should be better way to avoid hanging the threads. Some developers found a way. Browsers does not freez, media players does not freez, torrents are keep getting downloaded while you are holding mouse button. But as you can see in my example, not just drawing, but everything stops.
So me and everyone else here is open for that better solution :D
The only permanent solution is to stop using timers as threads and do a proper multithreading application.
By the way you are way way wrong about at least browsers if they use the default windows buttons they do stop updating. test it your self go to a slow loading page press ctrl+r to reload it and click on the minimize button the browser will freeze until you release that mouse button.
« Last Edit: August 10, 2018, 10:52:27 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

bobix

  • Jr. Member
  • **
  • Posts: 70
    • http://rechnik-bg.com
Re: [SOLVED]App freezing/stalling when button on title bar is clicked
« Reply #6 on: August 10, 2018, 10:52:48 am »
Quote
the only permanent solution is to stop using timers as threads and do a proper multithreading application
Button2 in my example is starting a thread. Or i'm not doing it right?
Lazarus 1.6.2 r53354 FPC 3.0.0 i386-win32-win32/win64

bobix

  • Jr. Member
  • **
  • Posts: 70
    • http://rechnik-bg.com
Re: [SOLVED]App freezing/stalling when button on title bar is clicked
« Reply #7 on: August 10, 2018, 10:55:36 am »
Quote
By the way you are way way wrong about at least browsers if they use the default windows buttons they do stop updating. test it your self go to a slow loading page press ctrl+r to reload it and click on the minimize button the browser will freeze until you release that mouse button.
I am opening cnn.com. I'm holding the minimize botton before even my browser connects to their server. In 5 seconds the web page is completely loaded in front of my eyes while i still holding minimize button.

Correction.
Chrome & IE freez while drawing, but still getting page in background.
Firefox & Edge does not freez in any kind.
« Last Edit: August 10, 2018, 11:05:00 am by bobix »
Lazarus 1.6.2 r53354 FPC 3.0.0 i386-win32-win32/win64

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: [SOLVED]App freezing/stalling when button on title bar is clicked
« Reply #8 on: August 10, 2018, 11:18:38 am »
Quote
By the way you are way way wrong about at least browsers if they use the default windows buttons they do stop updating. test it your self go to a slow loading page press ctrl+r to reload it and click on the minimize button the browser will freeze until you release that mouse button.
I am opening cnn.com. I'm holding the minimize botton before even my browser connects to their server. In 5 seconds the web page is completely loaded in front of my eyes while i still holding minimize button.

Correction.
Chrome & IE freez while drawing, but still getting page in background.
Firefox & Edge does not freez in any kind.
firefox freezes while reloading lazarus forums when pressing down the minimize button for me, I'm on windows 7, firefox quantum 60.0 (64-bit) I have never used edge nor I care to sorry.
That of course is not the point of my post. You should stop using timers as threading mechanism its unhealthy.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

bobix

  • Jr. Member
  • **
  • Posts: 70
    • http://rechnik-bg.com
Re: [SOLVED]App freezing/stalling when button on title bar is clicked
« Reply #9 on: August 10, 2018, 11:27:42 am »
Windows 10, Firefox 61 here. Strange.
Anyway. Like i said few times, I am using threads!
Button1 - simple loop.
Button2 - THREAD!!! ;D
Button3 - Timer.

So I am USING THREADS. Give me an example app with not freezing thread even if its form freezes.
Lazarus 1.6.2 r53354 FPC 3.0.0 i386-win32-win32/win64

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: [SOLVED]App freezing/stalling when button on title bar is clicked
« Reply #10 on: August 10, 2018, 11:38:03 am »
So I am USING THREADS. Give me an example app with not freezing thread even if its form freezes.
any thread that does not use synchronize for example the following thread will keep on running regardless of the state of the main thread.

Code: Pascal  [Select]
  1. implementation
  2.  
  3. {$R *.lfm}
  4. type
  5.   TUETestThread = class(TThread)
  6.   protected
  7.     procedure Execute; override;
  8.     procedure Complete(Data: PtrInt);
  9.   end;
  10.  
  11. procedure TUETestThread.Complete(Data: PtrInt);
  12. begin
  13.   form1.Button2.Caption:='Threath: '+inttostr(Data);
  14. end;
  15.  
  16. procedure TUETestThread.Execute;
  17. var
  18.   Thr : PtrInt = 0;
  19. begin
  20.  while not Terminated do
  21.   begin
  22.     thr:=thr+1;
  23.     sleep(1);
  24.     Application.QueueAsyncCall(@Complete, Thr);
  25.   end;
  26. end;
  27.  
  28.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

bobix

  • Jr. Member
  • **
  • Posts: 70
    • http://rechnik-bg.com
Re: [SOLVED]App freezing/stalling when button on title bar is clicked
« Reply #11 on: August 10, 2018, 11:49:46 am »
Thats what i was asking for! thank you!  ;D
Lazarus 1.6.2 r53354 FPC 3.0.0 i386-win32-win32/win64