I'm playing with threads today, so I'm sure this is only the first of several questions I will ask.
I tried the simple code below, expecting both threads to run simultaneously. On my PC, the main thread executes almost immediately, then the second thread starts and runs really slowly (by comparison to the main thread). Regardless of the values I use in sleep (0, 1, 5, 50) , I get the same results - thread NewThread executes sequentially after MainThread.
From this Help comment on SLEEP, I expected NewThread to run in parallel with the main thread.
Can someone clarify for me what I'm misunderstanding?
I tried the simple code below, expecting both threads to run simultaneously. On my PC, the main thread executes almost immediately, then the second thread starts and runs really slowly (by comparison to the main thread). Regardless of the values I use in sleep (0, 1, 5, 50) , I get the same results - thread NewThread executes sequentially after MainThread.
From this Help comment on SLEEP, I expected NewThread to run in parallel with the main thread.
Pause the current thread of the application for a specified number of milliseconds (mSec), allowing other processes (or threads) to continue.
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 0 Loop Until iCount& > 2000 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 0 Loop Until iCount& > 2000 End Function
Comment