AddTimer not work...

Just starting out? Need help? Post your questions and find answers here.
Didaktik
User
User
Posts: 79
Joined: Fri Mar 14, 2014 2:12 pm

AddTimer not work...

Post by Didaktik »

The timer never triggered. I can not understand why!

Code: Select all


#loading_window = 0

Procedure Loading()
  
  CallDebugger
  Static img, Dim border.l(1)
  
  If img = 0
    
    img = LoadImage(#PB_Any, "loading.png")
    border(0) = RGB(0,0,160)
    border(1) = RGB(160,160,0)
    
  EndIf 
    
  StartDrawing( WindowOutput(#loading_window))  
    
  Repeat 
    h = 1 + Random(16)
    Box(0,0,320,h, border(a))
    a ! 1
    y + h
  Until y > 240
  
  
  
  DrawImage(ImageID(img), 32,24)
  StopDrawing()
  
EndProcedure  

Procedure StartLoading()
  
  Static start = 0
    
  If start = 0
    
    OpenWindow(#loading_window, 0,0,320,240,"",#PB_Window_BorderLess|#PB_Window_ScreenCentered )
    
    Delay(500)
    
    AddWindowTimer(#loading_window, 1, 25)
    BindEvent(#PB_Event_Timer, @Loading());, #loading_window )
    start = 1
        
  Else
    
    RemoveWindowTimer(#loading_window, 2)
    CloseWindow(#loading_window)
    
  EndIf   
  
  
EndProcedure

StartLoading()   

Delay(5000)

Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: AddTimer not work...

Post by Marc56us »

There is not main loop and no call for event so BindEvent never receives anything :)

Avoid using Delay(), this is not the good way, Delay() stop all.

Last lines:

Code: Select all

;Delay(5000)

Repeat : WaitWindowEvent() : ForEver
:wink:
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: AddTimer not work...

Post by infratec »

In general:

A window program in a multitasking environment is always event driven.
So you need somewhere a loop which processes the events.

And when you use Bindevent() there is also a need for something which processes the events.

In PB the event processing is handled by WindowEvent() or WaitWindowEvent()
Which means you need to call one of them in a loop.

And as hint for further stuff:
Use only one place where you call WaitWindowEvent()
Else you may loose some events at the other location.

Even if you write a program with multiple windows: only use one place for the event loop.

Bernd
Didaktik
User
User
Posts: 79
Joined: Fri Mar 14, 2014 2:12 pm

Re: AddTimer not work...

Post by Didaktik »

I have a block with WaitEvent.

But how would I have wanted to isolate the creation and closing of windows in a separate procedure?

I do not want to have a bunch of messy code in the main body.
How to be?
Didaktik
User
User
Posts: 79
Joined: Fri Mar 14, 2014 2:12 pm

Re: AddTimer not work...

Post by Didaktik »

By the way. How can I do waitevent if this window is used to display during loading and initializing the main program.
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: AddTimer not work...

Post by infratec »

You can already be in your main event loop.

Then you can wait for an own event of your loading window program to show the main window.

You have to think a bit different when you coding event driven window programs.

Bernd
Didaktik
User
User
Posts: 79
Joined: Fri Mar 14, 2014 2:12 pm

Re: AddTimer not work...

Post by Didaktik »

I try to formulate the task:

I have a program, opens a window will ship a lot of files, creates a lot of gedgets etc.
It lasts about 5 seconds.

While this is happening, I wanted to open a small window and display it loading animation.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: AddTimer not work...

Post by Marc56us »

Like a splashscreen ?

Pseudo-code:
  1. Open main window (OpenWindow(#WinMain, ... #PB_Window_Invisible
  2. Hide-it Use #PB_Window_Invisible in OpenWindow line
  3. Open a second window for splashscreen OpenWindow(#WinSplash, (borderless)
  4. Display something in splashcreen (progressbar, text etc)
  5. Create gadget for main window (whatever there are slow, this is hide)
  6. When all initialization are done
  7. Close window splashscreen (CloseWindow(#WinSplash
  8. Unhide main window HideWindow(#WinMain, #False)
With this technique, no matter how long it takes to initialize the main window, it will appear only when everything is ready.
CloseWindow(#WinSplash is the last line of initialisation
(this is how I do in my little software the splashscreen is display will loading INI file, but main window is here (but hide))

:wink:
Last edited by Marc56us on Thu Oct 06, 2016 4:23 pm, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: AddTimer not work...

Post by infratec »

Hi,

Code: Select all

EnableExplicit


Enumeration #PB_Event_FirstCustomValue
  #Own_Event_InitFinished
EndEnumeration



Procedure DoInit(*Dummy)
  
  Delay(5000)
  
  PostEvent(#Own_Event_InitFinished)
  
EndProcedure



Define.i Event, Exit


OpenWindow(0, 0, 0, 400, 300, "Main", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Invisible)

OpenWindow(1, 0, 0, 200, 150, "Loading", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


CreateThread(@DoInit(), #Null)



Repeat
  
  Event = WaitWindowEvent()
  
  Select Event
    Case #Own_Event_InitFinished
      CloseWindow(1)
      HideWindow(0, #False)
      
    Case #PB_Event_CloseWindow
      Exit = #True
      
  EndSelect
  
Until Exit
Bernd
Didaktik
User
User
Posts: 79
Joined: Fri Mar 14, 2014 2:12 pm

Re: AddTimer not work...

Post by Didaktik »

infratec wrote:Hi,

Code: Select all

EnableExplicit


Enumeration #PB_Event_FirstCustomValue
  #Own_Event_InitFinished
EndEnumeration

Ok, but now I need to do ThreadSafe application. A ThreadSafe programs run slower than usual. It turns due SplahScreen entire program will then run slowly? )

__________________________________________________
Quote tag repaired
07.10.2016
RSBasic
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: AddTimer not work...

Post by Marc56us »

Without thread version

Code: Select all

; Sample of simple Splash screen system
; Marc56us - 2016-10-06

Enumeration 
     #Win_Main
     #Win_Splash
     #Load_Report
EndEnumeration

; --- Open main window first with *** #PB_Window_Invisible *** option
; --- Because some objects like timer are attached on it

OpenWindow(#Win_Main, 100, 100, 500, 300, "Hello World", 
           #PB_Window_SystemMenu| #PB_Window_Invisible)

OpenWindow(#Win_Splash, 0, 0, 300, 100, "", 
           #PB_Window_ScreenCentered | #PB_Window_BorderLess)

TextGadget(#Load_Report, 10, 40, 280, 80, "Loading...", #PB_Text_Center)

; --- Initialization...
; --- Now create object for #Win_Main and others action
; --- Display what you do
; --- Add delay only if user need to read it
; --- Sample
SetGadgetText(#Load_Report, "Load INI datas")          : Delay(1000)
; --- add call for Procedure to load prefs
SetGadgetText(#Load_Report, "Create Interface")        : Delay(1000)
; --- add call for Procedure for create objects
; etc.
SetGadgetText(#Load_Report, "Drink my coffee... ;-)")  : Delay(3000)
SetGadgetText(#Load_Report, "Oh oh? Phone call, wait") : Delay(2000)
SetGadgetText(#Load_Report, "Done Spam call :-]")      : Delay(2000)
SetGadgetText(#Load_Report, "Yes, now Ready.")         : Delay(1000)
; --- That's it.
CloseWindow(#Win_Splash)

; --- unhide main window
HideWindow(#Win_Main, #False)

While WaitWindowEvent() <> #PB_Event_CloseWindow : Wend
:wink:
Didaktik
User
User
Posts: 79
Joined: Fri Mar 14, 2014 2:12 pm

Re: AddTimer not work...

Post by Didaktik »

Marc56us wrote:Without thread version

:wink:
Thx!
But... loading have animation.
Like ZX Spectrum screen loading. :)

Image
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: AddTimer not work...

Post by Marc56us »

Didaktik wrote: But... loading have animation.
Like ZX Spectrum screen loading. :)

Image
Put animation on a Window and splashscreen on another window (over)
Uses StickyWindow(#Win_Splash, #True) to keep splash windows on top.

:wink:
Didaktik
User
User
Posts: 79
Joined: Fri Mar 14, 2014 2:12 pm

Re: AddTimer not work...

Post by Didaktik »

Marc56us wrote: Put animation on a Window and splashscreen on another window (over)
Uses StickyWindow(#Win_Splash, #True) to keep splash windows on top.

:wink:
and you joker :D
I animation is created in real time.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: AddTimer not work...

Post by wilbert »

As far as I know, you shouldn't make changes to the gui from outside the main thread.
So you might want to consider to reverse things; handle the animation from the main event loop and do the initialization and loading from another thread.
You could also try to optimize the initialization so it doesn't take that long (5 seconds seems much to me).
If I remember correctly it's also not a very good idea to use WindowOutput. It's better to use an ImageGadget or CanvasGadget. Drawing to WindowOutput is slower.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply