Hey, guys! I've wrote a simple programme which is a window showing DATE and TIME second by second using FreeBASIC and FLTK. BUT, The time didn't go second by second (sometime did sometime no). Is there anything wrong with my code? Could someone kindly correct it for me. Thank you.
Here is the code:
#include once "fltk-c.bi"
#include once "fbthread.bi"
#include once "vbcompat.bi"
DIM Shared AS Fl_Window ptr Window_Main
DIM Shared AS Fl_Widget ptr Box1
DIM Shared AS integer Win_Lenth
DIM Shared AS integer Win_Height
DIM Shared AS String MytimeString
DIM Shared AS Any Ptr MythreadNO
Fl_SetFont (18,"SimHei")
Win_Lenth = 800
Win_Height =600
Sub Create_Window_Main ( )
Window_Main = Fl_WindowNew2 (Fl_GetW()/2-Win_Lenth/2,Fl_GetH()/2-Win_Height/2,Win_Lenth,Win_Height,"测试")
Box1 = Fl_BoxNew (100,110,800,600)
Fl_WidgetSetLabelFont (Box1,18)
Fl_WidgetSetLabelSize (Box1,16)
END SUB
Sub myhandler (byval userdata as any ptr)
Dim AS double mytimeNOW
Dim AS string MyYil, MyAy, MyKun, MyHour, MyMinute, MySecond
mytimeNOW = NOW ()
MyYil = str (Year (mytimeNOW))
MyAy = str (Month (mytimeNOW))
MyKun = str (Day (mytimeNOW))
MyHour= str (Hour (mytimeNOW))
MyMinute=str (Minute (mytimeNOW))
MySecond=str (Second (mytimeNOW))
MytimeString = "现在是:"+MyYil+"年"+MyAy+"月"+MyKun+"日 "+MyHour+"时"+MyMinute+"分"+MySecond+"秒"
Fl_WidgetSetLabel (Box1, MytimeString)
END SUB
Sub mythread (byval userdata as any ptr)
Start:
Fl_RepeatTimeout(1, @myhandler)
GOTO Start
END SUB
Create_Window_Main ( )
Fl_WindowShow (Window_Main)
MythreadNO = ThreadCreate(@mythread, 0)
Fl_Run
END
#Include Once "fltk-c.bi"
#Include Once "fbthread.bi"
#Include Once "vbcompat.bi"
Dim Shared As Fl_Window Ptr Window_Main
Dim Shared As Fl_Widget Ptr Box1
Dim Shared As Integer Win_Lenth
Dim Shared As Integer Win_Height
Dim Shared As String MytimeString
Dim Shared As Any Ptr MythreadNO
Fl_SetFont (18,"SimHei")
Win_Lenth = 800
Win_Height =600
Sub Create_Window_Main ( )
Window_Main = Fl_WindowNew2 (Fl_GetW()/2-Win_Lenth/2,Fl_GetH()/2-Win_Height/2,Win_Lenth,Win_Height,"??")
Box1 = Fl_BoxNew (100,110,800,600)
Fl_WidgetSetLabelFont (Box1,18)
Fl_WidgetSetLabelSize (Box1,16)
End Sub
Sub myhandler (ByVal userdata As Any Ptr)
Dim As Double mytimeNOW = Now()
Dim As Integer MyYil, MyAy, MyKun, MyHour, MyMinute, MySecond
MyYil = Year (mytimeNOW)
MyAy = Month (mytimeNOW)
MyKun = Day (mytimeNOW)
MyHour= Hour (mytimeNOW)
MyMinute = Minute (mytimeNOW)
MySecond = Second (mytimeNOW)
MytimeString = "???:" & MyYil & "?" & MyAy & "?" & MyKun & "? " & MyHour & "?" & MyMinute & "?" & MySecond & "?"
Fl_WidgetSetLabel (Box1, MytimeString)
End Sub
Sub mythread (ByVal userdata As Any Ptr)
'Start:
'GoTo Start
While Fl_Wait()
Fl_RepeatTimeout(1,Cast (Any Ptr,@myhandler))
Sleep 1000\60,1
Wend
End Sub
Create_Window_Main ( )
Fl_WindowShow (Window_Main)
MythreadNO = ThreadCreate(@mythread, 0)
Fl_Run
End
Yep, without sleep, the computer is probably to busy looping (throwing all its computing power at this loop), no time for graphics updates. Sleep 1,1 is probably also OK.
Edit: Not sure if sleep is needed if Fl_wait is also used.
Last edited by badidea on Aug 07, 2018 18:29, edited 1 time in total.
I've REMed some lines with Chinese characters and font settings. Your computer may have not got the Chinese font I used, so you got a grey window with nothing showing on it.
#include once "fltk-c.bi"
#include once "fbthread.bi"
#include once "vbcompat.bi"
DIM Shared AS Fl_Window ptr Window_Main
DIM Shared AS Fl_Widget ptr Box1
DIM Shared AS integer Win_Lenth
DIM Shared AS integer Win_Height
DIM Shared AS String MytimeString
DIM Shared AS Any Ptr MythreadNO
REM Fl_SetFont (18,"SimHei")
Win_Lenth = 800
Win_Height =600
Sub Create_Window_Main ( )
Window_Main = Fl_WindowNew2 (Fl_GetW()/2-Win_Lenth/2,Fl_GetH()/2-Win_Height/2,Win_Lenth,Win_Height,"TEST")
Box1 = Fl_BoxNew (100,110,800,600)
REM Fl_WidgetSetLabelFont (Box1,18)
Fl_WidgetSetLabelSize (Box1,16)
END SUB
Sub myhandler (byval userdata as any ptr)
Dim AS double mytimeNOW
Dim AS string MyYil, MyAy, MyKun, MyHour, MyMinute, MySecond
mytimeNOW = NOW ()
MyYil = str (Year (mytimeNOW))
MyAy = str (Month (mytimeNOW))
MyKun = str (Day (mytimeNOW))
MyHour= str (Hour (mytimeNOW))
MyMinute=str (Minute (mytimeNOW))
MySecond=str (Second (mytimeNOW))
MytimeString = "NOW YEAR "+MyYil+" Month "+MyAy+" Day "+MyKun+" Hour "+MyHour+" Minute "+MyMinute+" Second"+MySecond
Fl_WidgetSetLabel (Box1, MytimeString)
END SUB
Sub mythread (byval userdata as any ptr)
Start:
Fl_RepeatTimeout(1, @myhandler)
GOTO Start
END SUB
Create_Window_Main ( )
Fl_WindowShow (Window_Main)
MythreadNO = ThreadCreate(@mythread, 0)
Fl_Run
END
[xcb] Unknown request in queue while dequeuing
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
clock: ../../src/xcb_io.c:179: dequeue_pending_request: Assertion `!xcb_xlib_unknown_req_in_deq' failed.
Aborted (core dumped)
badidea wrote:Yep, without sleep, the computer is probably to busy looping (throwing all its computing power at this loop), no time for graphics updates. Sleep 1,1 is probably also OK.
Edit: Not sure if sleep is needed if Fl_wait is also used.
[xcb] Unknown request in queue while dequeuing
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
clock: ../../src/xcb_io.c:179: dequeue_pending_request: Assertion `!xcb_xlib_unknown_req_in_deq' failed.
Aborted (core dumped)
I've found what is wrong with the program by surfing internet day by day, reading articles about THREAD and FLTK.
I've found the best solution by surfing the Internet day by day. I combined my previous code with WQ1980's code. NOW, this programme can be compiled on both Windows and Linux machines, and works fine(just as my wish - parsing time second by second).
REM (Windows) fbc -s gui xxx.bas
REM (Linux) fbc -s gui xxx.bas -l X11
#include once "fltk-c.bi"
#include once "vbcompat.bi"
#IFDEF __FB_LINUX__
#include once "X11/Xlib.bi"
#include once "crt/pthread.bi"
#ENDIF
DIM Shared AS Fl_Window ptr Window_Main
DIM Shared AS Fl_Widget ptr Box1
DIM Shared AS integer Win_Lenth
DIM Shared AS integer Win_Height
DIM Shared AS String MytimeString
DIM Shared AS Any Ptr MythreadNO
#IFDEF __FB_LINUX__
XInitThreads()
#ENDIF
Win_Lenth = 800
Win_Height =600
Sub Create_Window_Main ( )
Window_Main = Fl_WindowNew2 (Fl_GetW()/2-Win_Lenth/2,Fl_GetH()/2-Win_Height/2,Win_Lenth,Win_Height,"Test")
Box1 = Fl_BoxNew (100,110,800,600)
Fl_WidgetSetLabelSize (Box1,16)
END SUB
Sub myhandler (byval userdata as any ptr)
Dim AS double mytimeNOW
Dim AS string MyYil, MyAy, MyKun, MyHour, MyMinute, MySecond
mytimeNOW = NOW ()
MyYil = str (Year (mytimeNOW))
MyAy = str (Month (mytimeNOW))
MyKun = str (Day (mytimeNOW))
MyHour= str (Hour (mytimeNOW))
MyMinute=str (Minute (mytimeNOW))
MySecond=str (Second (mytimeNOW))
MytimeString = "NOW is: "+MyYil+Chr(45)+MyAy+Chr(45)+MyKun+" "+MyHour+Chr(58)+MyMinute+Chr(58)+MySecond
Fl_WidgetSetLabel (Box1, MytimeString)
END SUB
#IFDEF __FB_LINUX__
Sub mythread (byval userdata as any ptr)
Start:
Fl_RepeatTimeout(1, @myhandler)
sleep 1000\20, 1
GOTO Start
END SUB
#ENDIF
#IFDEF __FB_WIN32__
Sub mythread (ByVal userdata As Any Ptr)
'Start:
'GoTo Start
While Fl_Wait()
Fl_RepeatTimeout(1,Cast (Any Ptr,@myhandler))
Sleep 1000\20, 1
Wend
End Sub
#ENDIF
Create_Window_Main ( )
Fl_WindowShow (Window_Main)
MythreadNO = ThreadCreate(@mythread, 0)
Fl_Run
END
REM 编译方法(Windows) fbc -s gui xxx.bas
REM 编译方法(Linux) fbc -s gui xxx.bas -l X11
#include once "fltk-c.bi"
#include once "vbcompat.bi"
#IFDEF __FB_LINUX__
#include once "X11/Xlib.bi"
#include once "crt/pthread.bi"
#ENDIF
DIM Shared AS Fl_Window ptr Window_Main
DIM Shared AS Fl_Widget ptr Box1
DIM Shared AS integer Win_Lenth
DIM Shared AS integer Win_Height
DIM Shared AS String MytimeString
DIM Shared AS Any Ptr MythreadNO
#IFDEF __FB_LINUX__
XInitThreads()
#ENDIF
Fl_SetFont (18,"SimHei")
Win_Lenth = 800
Win_Height =600
Sub Create_Window_Main ( )
Window_Main = Fl_WindowNew2 (Fl_GetW()/2-Win_Lenth/2,Fl_GetH()/2-Win_Height/2,Win_Lenth,Win_Height,"测试")
Box1 = Fl_BoxNew (100,110,800,600)
Fl_WidgetSetLabelFont (Box1,18)
Fl_WidgetSetLabelSize (Box1,16)
END SUB
Sub myhandler (byval userdata as any ptr)
Dim AS double mytimeNOW
Dim AS string MyYil, MyAy, MyKun, MyHour, MyMinute, MySecond
mytimeNOW = NOW ()
MyYil = str (Year (mytimeNOW))
MyAy = str (Month (mytimeNOW))
MyKun = str (Day (mytimeNOW))
MyHour= str (Hour (mytimeNOW))
MyMinute=str (Minute (mytimeNOW))
MySecond=str (Second (mytimeNOW))
MytimeString = "现在是:"+MyYil+"年"+MyAy+"月"+MyKun+"日 "+MyHour+"时"+MyMinute+"分"+MySecond+"秒"
Fl_WidgetSetLabel (Box1, MytimeString)
END SUB
#IFDEF __FB_LINUX__
Sub mythread (byval userdata as any ptr)
Start:
Fl_RepeatTimeout(1, @myhandler)
sleep 1000\20, 1
GOTO Start
END SUB
#ENDIF
#IFDEF __FB_WIN32__
Sub mythread (ByVal userdata As Any Ptr)
'Start:
'GoTo Start
While Fl_Wait()
Fl_RepeatTimeout(1,Cast (Any Ptr,@myhandler))
Sleep 1000\20, 1
Wend
End Sub
#ENDIF
Create_Window_Main ( )
Fl_WindowShow (Window_Main)
MythreadNO = ThreadCreate(@mythread, 0)
Fl_Run
END
Just a reminder:
the thread, you are starting (the one extern, to FLTK) should still be stopped,
before program exit, unless you want to on purpose, create memory leaks ...
Using either:
ThreadDetach(), or ThreadWait()
Then:
using a Mutex, is also considered good practice, whenever threads are involved.
See relevant parts in the FB-manual and the attached examples.