Page 1 of 1

how to show sub-window in a Thread ?

Posted: Sat Aug 17, 2024 4:46 am
by hdt888
I create a message sub-window, to replace the MessageRequester() and I want to call this window in a Thread and stop the Thread when this window appears.

How ?
Thank you so much !

Re: how to show sub-window in a Thread ?

Posted: Sat Aug 17, 2024 9:03 am
by infratec
Use PostEvent to tell the main thread to show the window and use WaitSemaphore to wait until the window is closed.

Re: how to show sub-window in a Thread ?

Posted: Sat Aug 17, 2024 10:23 am
by TI-994A
hdt888 wrote: Sat Aug 17, 2024 4:46 am...message sub-window, to replace the MessageRequester() and I want to call this window in a Thread and stop the Thread when this window appears.
Perhaps something like this:

Code: Select all

EnableExplicit

Enumeration windows
  #mainWindow
  #messageBox  
EndEnumeration

Enumeration gadgets
  #mainWindowButton  
  #mainWindowClock
  #mainWindowTimer
  #mainWindowStatusBar
  #messageBoxButton1
  #messageBoxButton2
  #messageBoxText
EndEnumeration

#threadTimeout = 60000 
#threadTrigger = 5000
#customThreadEvent = #PB_Event_FirstCustomValue
#customThreadEventType = #PB_EventType_FirstCustomValue

Define event, appQuit, threadElapsed, threadTerminate, time.s

Global msgBox, thread, threadTimer, threadTrigger, threadQuit
Global mainWindowFlags = #PB_Window_SystemMenu |
                         #PB_Window_SizeGadget |                
                         #PB_Window_ScreenCentered |
                         #PB_Window_MaximizeGadget |
                         #PB_Window_MinimizeGadget

Macro setStatus(field, message)
  StatusBarText(#mainWindowStatusBar, field, message)
EndMacro

Procedure messageBox(title.s, message.s)
  OpenWindow(#messageBox, 10, 10, 200, 150, title, 
             #PB_Window_WindowCentered, WindowID(#mainWindow))  
  TextGadget(#messageBoxText, 10, 30, 180, 100, message, #PB_Text_Center)
  ButtonGadget(#messageBoxButton1, 10, 100, 100, 30, "Kill Thread")
  ButtonGadget(#messageBoxButton2, 110, 100, 80, 30, "Cancel")  
  StickyWindow(#messageBox, #True)
EndProcedure

Procedure threadedProcess(null)   
  
  ; simulating some thread process through elapsed time
  threadTimer = ElapsedMilliseconds()
  threadTrigger = threadTimer
  
  ; the thread will auto-terminate in 60 seconds
  While Not threadQuit And 
        ElapsedMilliseconds() - threadTimer < #threadTimeout            
    
    ; the custom thread event will be posted every
    ; 5 seconds as long as the thread is running
    If ElapsedMilliseconds() - threadTrigger = #threadTrigger
      threadTrigger = ElapsedMilliseconds()
      PostEvent(#customThreadEvent)      
    EndIf    
    
  Wend
  
  PostEvent(#PB_Event_Gadget, #messageBox, #messageBoxButton1, #customThreadEventType)
  
EndProcedure

Procedure mainwindow()  
  OpenWindow(#mainWindow, 0, 0, 800, 400,"Main Window", mainWindowFlags)
  TextGadget(#mainWindowClock, 10, 10, 780, 50, "00:00:00", #PB_Text_Right)
  ButtonGadget(#mainWindowButton, 300, 60, 200, 30, "Start threaded process...")  
  AddWindowTimer(#mainWindow, #mainWindowTimer, 1000)  
  
  If CreateStatusBar(#mainWindowStatusBar, WindowID(#mainWindow))
    AddStatusBarField(200) : AddStatusBarField(200) 
    AddStatusBarField(200) : AddStatusBarField(200) 
    setStatus(0, "   thread status: not active")
    setStatus(1, "message box: not displayed")
    setStatus(2, "thread event: 0 seconds")
    setStatus(3, "thread terminate: 0 seconds")
  EndIf

EndProcedure

mainwindow()

Repeat
  event = WaitWindowEvent()
  Select EventWindow()
      
    ;handles all the events for the main window  
    Case #mainWindow
      Select event
        Case #PB_Event_CloseWindow
          appQuit = 1
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #mainWindowButton
              threadQuit = #False
              thread = CreateThread(@threadedProcess(), 0)              
              DisableGadget(#mainWindowButton, #True)              
              setStatus(0, "   thread status: active") 
          EndSelect
        Case #PB_Event_Timer
          Select EventTimer()
            Case #mainWindowTimer              
              time.s = FormatDate("%hh:%ii:%ss", Date())
              SetGadgetText(#mainWindowClock, time)     
              If IsThread(thread) 
                If Not msgBox
                  threadElapsed = (#threadTrigger - (ElapsedMilliseconds() - threadTrigger)) / 1000     
                  setStatus(2, "thread event: " +  Str(threadElapsed) + " seconds")
                EndIf         
                threadTerminate = (#threadTimeout - (ElapsedMilliseconds() - threadTimer)) / 1000
                setStatus(3, "thread terminate: " + threadTerminate + " seconds")
              EndIf              
          EndSelect     
          
        ;handles the custom thread event   
        Case #customThreadEvent
          If Not msgBox
            messageBox("Custom Message Box", "Thread event triggered!")
            setStatus(1, "message box: displayed")
            msgBox = #True
          EndIf           
          
      EndSelect
      
    ;handles all the events for the message box window   
    Case #messageBox
      Select event
        Case #PB_Event_CloseWindow
          CloseWindow(#messageBox)
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #messageBoxButton1                           
              threadQuit = #True                            
              DisableGadget(#mainWindowButton, #False)
              setStatus(0, "   thread status: not active")
              setStatus(2, "thread event: 0 seconds")              
              If msgBox  
                CloseWindow(#messageBox)                                  
              EndIf
              msgBox = #False
            Case #messageBoxButton2
              msgBox = #False              
              threadTrigger = ElapsedMilliseconds()
              CloseWindow(#messageBox)
          EndSelect          
          setStatus(1, "message box: not displayed")
      EndSelect              
      
  EndSelect
Until appQuit

Re: how to show sub-window in a Thread ?

Posted: Sat Aug 17, 2024 11:17 am
by RASHAD
Why Thread ?
Why not Procedure() or Module and Modal Window with OK Cancel?

Re: how to show sub-window in a Thread ?

Posted: Sat Aug 17, 2024 1:58 pm
by jacdelad
If possible, create the window beforehand and make it visible when needed.

Re: how to show sub-window in a Thread ?

Posted: Tue Aug 20, 2024 11:48 am
by netmaestro
"Purebasic Doc" wrote:Important: A window should not be opened in a thread, as there is some limitation on OS X and Linux. A debugger error will be raised.