how to show sub-window in a Thread ?

Just starting out? Need help? Post your questions and find answers here.
hdt888
User
User
Posts: 47
Joined: Sun Jul 07, 2024 8:42 am

how to show sub-window in a Thread ?

Post 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 !
PB 5.x + 6.x + Win10. Feel the ...Pure... Power.
infratec
Always Here
Always Here
Posts: 7627
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post by infratec »

Use PostEvent to tell the main thread to show the window and use WaitSemaphore to wait until the window is closed.
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

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

Post 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
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4955
Joined: Sun Apr 12, 2009 6:27 am

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

Post by RASHAD »

Why Thread ?
Why not Procedure() or Module and Modal Window with OK Cancel?
Egypt my love
User avatar
jacdelad
Addict
Addict
Posts: 2015
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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

Post by jacdelad »

If possible, create the window beforehand and make it visible when needed.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

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

Post 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.
BERESHEIT
Post Reply