Page 1 of 2
AddTimer not work...
Posted: Thu Oct 06, 2016 1:43 pm
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)
Re: AddTimer not work...
Posted: Thu Oct 06, 2016 1:52 pm
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

Re: AddTimer not work...
Posted: Thu Oct 06, 2016 2:37 pm
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
Re: AddTimer not work...
Posted: Thu Oct 06, 2016 3:05 pm
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?
Re: AddTimer not work...
Posted: Thu Oct 06, 2016 3:09 pm
by Didaktik
By the way. How can I do waitevent if this window is used to display during loading and initializing the main program.
Re: AddTimer not work...
Posted: Thu Oct 06, 2016 3:15 pm
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
Re: AddTimer not work...
Posted: Thu Oct 06, 2016 3:34 pm
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.
Re: AddTimer not work...
Posted: Thu Oct 06, 2016 3:55 pm
by Marc56us
Like a
splashscreen ?
Pseudo-code:
- Open main window (OpenWindow(#WinMain, ... #PB_Window_Invisible
- Hide-it Use #PB_Window_Invisible in OpenWindow line
- Open a second window for splashscreen OpenWindow(#WinSplash, (borderless)
- Display something in splashcreen (progressbar, text etc)
- Create gadget for main window (whatever there are slow, this is hide)
- When all initialization are done
- Close window splashscreen (CloseWindow(#WinSplash
- 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))

Re: AddTimer not work...
Posted: Thu Oct 06, 2016 4:05 pm
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
Re: AddTimer not work...
Posted: Thu Oct 06, 2016 4:42 pm
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
Re: AddTimer not work...
Posted: Thu Oct 06, 2016 4:48 pm
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

Re: AddTimer not work...
Posted: Thu Oct 06, 2016 5:04 pm
by Didaktik
Marc56us wrote:Without thread version

Thx!
But... loading have animation.
Like ZX Spectrum screen loading.

Re: AddTimer not work...
Posted: Thu Oct 06, 2016 6:00 pm
by Marc56us
Didaktik wrote:
But... loading have animation.
Like ZX Spectrum screen loading.

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

Re: AddTimer not work...
Posted: Thu Oct 06, 2016 9:22 pm
by Didaktik
and you joker
I animation is created in real time.
Re: AddTimer not work...
Posted: Fri Oct 07, 2016 6:24 am
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.