Why is the Clock stopping when the MessageRequester dialog box appears ?

Just starting out? Need help? Post your questions and find answers here.
hoangdiemtinh
User
User
Posts: 97
Joined: Wed Nov 16, 2022 1:51 pm

Why is the Clock stopping when the MessageRequester dialog box appears ?

Post by hoangdiemtinh »

I am new to PB.
My primary language is not US/UK. I am using Google Translate.

Why is the Clock stopping when the MessageRequester dialog box appears ? How to fix this ? Thank you.

Code: Select all

TextGadget(#text_CLOCK, 10, 10, 80, 20, "8:18:18", #PB_Text_Center | #SS_CENTERIMAGE)
AddWindowTimer(#main_Window, #Timer_1, 1000)
.....
.....
Case #PB_Event_Timer
          SetGadgetText(#text_CLOCK, FormatDate("%hh:%ii:%ss", Date()))
Last edited by hoangdiemtinh on Tue May 21, 2024 2:57 am, edited 1 time in total.
PC: Windows 10 x64, 8GB RAM. PB ver: 6.x
--
I love PB5 vs PB6 :)
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: Why is the Clock stopping when the MessageRequester dialog box appears ?

Post by AZJIO »

A modal window in a graphical user interface is a window that blocks the user from interacting with its parent application until the user closes the window.
hoangdiemtinh
User
User
Posts: 97
Joined: Wed Nov 16, 2022 1:51 pm

Re: Why is the Clock stopping when the MessageRequester dialog box appears ?

Post by hoangdiemtinh »

@AZJIO, Is there a solution, like Thread, to fix this problem ?
PC: Windows 10 x64, 8GB RAM. PB ver: 6.x
--
I love PB5 vs PB6 :)
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: Why is the Clock stopping when the MessageRequester dialog box appears ?

Post by AZJIO »

1. You can call cmd.exe and pass text for output

Code: Select all

Title$ = "Title"
Message$ = "Message"
tmp$ = "Title " + Title$ + " & @Echo off & @Echo. & Color 1e & @Echo. " + Message$ + "& "
tmp$ = EscapeString(tmp$)
RunProgram("cmd.exe", "/c (" + tmp$ + " set /p Ok=^>^>)", "")
2. You can compile a small file using a MessageRequester in it, where the text is transmitted via the command line.

Code: Select all

Define Title$
Define Message$
Define flag
Define Count = CountProgramParameters()
If Count > 0
	Message$ = ProgramParameter(0)
	If Count > 1
		Title$ = ProgramParameter(1)
		If Count > 2
			flag = Val(ProgramParameter(2))
		EndIf
	EndIf
	MessageRequester(Title$, Message$, flag)
EndIf
3. You can make your own window, but do not make it modal, that is, not blocking the parent window.

Code: Select all

If OpenWindow(0, 0, 0, 520, 300, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	ButtonGadget(0, 10, 10, 120, 30, "Window")
	TextGadget(3, 10, 50, 210, 20, "0")
	If OpenWindow(1, 0, 0, 220, 100, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Invisible)
		TextGadget(2, 10, 10, 210, 20, "Hello")
		ButtonGadget(1, 10, 40, 120, 30, "Close")
		StickyWindow(1, #True) 
	EndIf
	AddWindowTimer(0, 0, 1000)
	
	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_Timer
				i+1
				SetGadgetText(3, Str(i))
			Case #PB_Event_Gadget
				Select EventGadget()
					Case 0
						HideWindow(1, #False)
					Case 1
						HideWindow(1, #True)
				EndSelect
			Case #PB_Event_CloseWindow
				wn = EventWindow()
				If wn
					HideWindow(1, #True)
				Else
					CloseWindow(0)
					CloseWindow(1)
					End
				EndIf
		EndSelect
	ForEver
EndIf
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Why is the Clock stopping when the MessageRequester dialog box appears ?

Post by BarryG »

Apparently you're not supposed to update gadgets from a thread, but this works. So until the compiler complains about it: sue me.

Code: Select all

Global WindowID, ButtonID, TextID, ClockThread

Procedure UpdateClock(param)
  Repeat
    SetGadgetText(TextID, FormatDate("%hh:%ii:%ss", Date()))
    Delay(500)
  ForEver
EndProcedure

WindowID = OpenWindow(0, 200, 200, 300, 100, "Clock Update Example", #PB_Window_SystemMenu)

ButtonID = ButtonGadget(#PB_Any, 50, 20, 200, 25, "Show MessageRequster")
TextID = TextGadget(#PB_Any, 50, 60, 200, 20, "", #PB_Text_Center)

ClockThread = CreateThread(@UpdateClock(), 0)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
      
    Case #PB_Event_Gadget
      If EventGadget() = ButtonID
        MessageRequester("Information", "This is a non-blocking message.")
      EndIf
      
  EndSelect
ForEver
hoangdiemtinh
User
User
Posts: 97
Joined: Wed Nov 16, 2022 1:51 pm

Re: Why is the Clock stopping when the MessageRequester dialog box appears ?

Post by hoangdiemtinh »

Thanks @BarryG.
Great code. The problem has been resolved.
PC: Windows 10 x64, 8GB RAM. PB ver: 6.x
--
I love PB5 vs PB6 :)
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 487
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Why is the Clock stopping when the MessageRequester dialog box appears ?

Post by Mindphazer »

BarryG wrote: Tue May 21, 2024 8:48 am Apparently you're not supposed to update gadgets from a thread, but this works. So until the compiler complains about it: sue me.
Hi Barry,
it does work on Windows, but does not on MacOS. That's why, unless the code is to be used on Windows only, you're not supposed to do it :mrgreen:
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Why is the Clock stopping when the MessageRequester dialog box appears ?

Post by BarryG »

I would've thought the compiler throw a warning message then, like it does if you use WindowEvent() in a thread? But it doesn't, so I assumed it's okay to use.
User avatar
Michael Vogel
Addict
Addict
Posts: 2819
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Why is the Clock stopping when the MessageRequester dialog box appears ?

Post by Michael Vogel »

Nothing complete new, just another option...

Code: Select all


Procedure Message(nil)
	
	MessageRequester("Hi","ho")
	PostEvent(9999)
	
EndProcedure

If OpenWindow(0, 0, 0, 520, 300, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	ButtonGadget(0, 10, 10, 120, 30, "Window")
	ButtonGadget(9,10,50,120,30,"Requester")
	TextGadget(3, 10, 100, 210, 20, "0")
	If OpenWindow(1, 0, 0, 220, 100, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Invisible)
		TextGadget(2, 10, 10, 210, 20, "Hello")
		ButtonGadget(1, 10, 40, 120, 30, "Close")
		StickyWindow(1, #True)
	EndIf
	AddWindowTimer(0, 0, 100)

	Repeat
		Select WaitWindowEvent()
		Case 9999
			DisableGadget(9,0)
		Case #PB_Event_Timer
			i+1
			SetGadgetText(3, StrF(i/10,1))
		Case #PB_Event_Gadget
			Select EventGadget()
			Case 0
				HideWindow(1, #False)
			Case 1
				HideWindow(1, #True)
			Case 9
				DisableGadget(9,1)
				CreateThread(@Message(),#Null)
			EndSelect
		Case #PB_Event_CloseWindow
			wn = EventWindow()
			If wn
				HideWindow(1, #True)
			Else
				CloseWindow(0)
				CloseWindow(1)
				End
			EndIf
		EndSelect
	ForEver
EndIf
Post Reply