Same code as my last message - different question, just trying to understand how Sleep works.
If I use Sleep 0, I expect the thread to give up the rest of the current time slice. I don't know what the time-slice for my PC, but if it is 20ms, then Sleep 0 would give up 0-20 ms, depending on how much of the current time slice the current thread had already used when Sleep was called.
If I use Sleep 1, then I expect it to give up only 1 ms of the current time slice.
If these two statements are true, then I would expect Sleep 0 would cause a thread to run more slowly than Sleep 1 (unless it just happens that Sleep 0 hits on the 19 ms mark). But that's not what I see in the code.
Can someone explain my mis-understanding of how Sleep is supposed to work?
If I use Sleep 0, I expect the thread to give up the rest of the current time slice. I don't know what the time-slice for my PC, but if it is 20ms, then Sleep 0 would give up 0-20 ms, depending on how much of the current time slice the current thread had already used when Sleep was called.
If I use Sleep 1, then I expect it to give up only 1 ms of the current time slice.
If these two statements are true, then I would expect Sleep 0 would cause a thread to run more slowly than Sleep 1 (unless it just happens that Sleep 0 hits on the 19 ms mark). But that's not what I see in the code.
Can someone explain my mis-understanding of how Sleep is supposed to work?
Code:
'Compilable Example: #Compile Exe #Dim All #Include "Win32API.inc" Global hDlg As Dword, hThread As Dword Function PBMain() As Long Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg Control Add Button, hDlg, 100,"Start Thread", 50,10,100,20 Control Add Label, hDlg, 200,"<main thread count>", 50,40,100,20 Control Add Label, hDlg, 300,"<extra thread count>", 50,70,100,20 Dialog Show Modal hDlg Call DlgProc End Function CallBack Function DlgProc() As Long If Cb.Msg = %WM_Command And Cb.Ctl = 100 And Cb.CtlMsg = %BN_Clicked Then Thread Create NewThread(0) To hThread 'start the new thread, argument not used Thread Close hThread To hThread 'suggested by PowerBASIC Inc. as good practice MainThread 'in the main thread, keep doing something End If End Function Function MainThread () As Long Local iCount& Do Incr iCount& : Control Set Text hDlg, 200, "Main" + Str$(iCount&) Sleep 1 Loop Until iCount& > 500 End Function Thread Function NewThread (ByVal x As Long) As Long Local iCount& Do Incr iCount& : Control Set Text hDlg, 300, "Thread" + Str$(iCount&) Sleep 1 Loop Until iCount& > 500 End Function
Comment