Custom Procedure - OpenWindow in Thread

Share your advanced PureBasic knowledge/code with the community.
Liqu
User
User
Posts: 77
Joined: Sun Apr 21, 2013 10:31 am

Custom Procedure - OpenWindow in Thread

Post by Liqu »

I hope this code will be useful for you :)

NB : Still have gadget Creation error, please wait for updated version

Code: Select all

;// Code by Liqu
;// Purebasic English 5.20 LTS
;// Unicode and ThreadSafe on

;// Enum Event
; Enumeration #PB_Event_FirstCustomValue
;   #MB_OpenWindowPostEvent
; EndEnumeration
#MB_OpenWindowPostEvent = #PB_Event_FirstCustomValue + 1000000

;// Structure for List
Structure MB_listOpenWindowSTR
  Window.i
  X.i
  Y.i
  Width.i
  Height.i
  Title$
  Flag.i
  ParentId.i
  mapWindow$
EndStructure
; 
;// Global Variable for MB_OpenWindowPostEvent
Global NewList MB_listOpenWindow.MB_listOpenWindowSTR()
Global NewMap MB_mapOpenWindow()
; 

Procedure MB_OpenWindowPostEvent(Window=#PB_Any, X=0, Y=0, Width=0, Height=0, Title$="", Flags=0, ParentID=0)
  Protected i, Result, mapWindow$
  
  ;// Add List Element for Creating Window
  If AddElement(MB_listOpenWindow())
    MB_listOpenWindow()\Window      = Window
    MB_listOpenWindow()\X           = X
    MB_listOpenWindow()\Y           = Y
    MB_listOpenWindow()\Width       = Width
    MB_listOpenWindow()\Height      = Height
    MB_listOpenWindow()\Title$      = Title$
    MB_listOpenWindow()\Flag        = Flags
    MB_listOpenWindow()\ParentId    = ParentID
  EndIf
  
  ;// Random Number for Map
  Repeat
    mapWindow$ = Str(Random(1000000,1))
    If Not FindMapElement(MB_mapOpenWindow(), mapWindow$)
      MB_listOpenWindow()\mapWindow$ = mapWindow$
      Break
    EndIf
  ForEver
    
  ;// PostEvent for Creating Window
  PostEvent(#MB_OpenWindowPostEvent)
  
  ;// Getting Return Value From Window Creation
  Repeat
    ;// Check If Map Already Inserted
    If FindMapElement(MB_mapOpenWindow(), mapWindow$)
      Result = MB_mapOpenWindow(mapWindow$)
    Else
      Result = -1
    EndIf
    
    ;// Delete Map
    If Result <> -1
      DeleteMapElement(MB_mapOpenWindow(),mapWindow$)
      Break
    EndIf
    Delay(1)
  ForEver
  
  ProcedureReturn Result
EndProcedure


Procedure MB_OpenWindowPostEventCREATE()
  
  ;// Loop Each Element List to Create Window
  ResetList(MB_listOpenWindow())
  ForEach MB_listOpenWindow()
    ;// Create Window and Store Result to Map
     If MB_listOpenWindow()\Window
      MB_mapOpenWindow(MB_listOpenWindow()\mapWindow$) = OpenWindow(MB_listOpenWindow()\Window,MB_listOpenWindow()\X,MB_listOpenWindow()\Y,MB_listOpenWindow()\Width,MB_listOpenWindow()\Height,MB_listOpenWindow()\Title$,MB_listOpenWindow()\Flag,MB_listOpenWindow()\ParentId)
      ;// Delete this element list
      DeleteElement(MB_listOpenWindow())
     EndIf
    Delay(1)
  Next
  
EndProcedure


;#--------------------------------------------
;- ### TEST EXAMPLE
;#--------------------------------------------

Procedure OpenXWindow(init=0)
  
  TimeStart = ElapsedMilliseconds()
  For i = 1 To 1000
    Debug Str(i) + " : "+ Str(MB_OpenWindowPostEvent(#PB_Any,0,0,1,1,Title$))
  Next
  TimeResult = ElapsedMilliseconds() - TimeStart
  ;MessageRequester("", "Speed TEST = "+Str(TimeResult) )
  Debug "Speed TEST = "+Str(TimeResult)
  
EndProcedure

OpenWindow(#PB_Any,0,0,1,1,"",#PB_Window_Invisible)
CreateThread(@OpenXWindow(),1)

Repeat
  Event = WaitWindowEvent()
  
  ;// Put this after your WaitWindowEvent() or WindowEvent()
  If Event = #MB_OpenWindowPostEvent
      MB_OpenWindowPostEventCREATE()
  EndIf
  
  
ForEver