I'm looking for a simple or easy to understand prog to run a separate process in a thread function. Anyone can spare some codes?
Announcement
Collapse
No announcement yet.
Need a simple prog to run a thread function
Collapse
X
-
This is about as simple and easy as I can come up with to demonstrate a thread doing something while the main application continues to do things:
'Code:#COMPILE EXE #DIM ALL #DEBUG ERROR ON #DEBUG DISPLAY ON THREAD FUNCTION T1(BYVAL x AS LONG) AS LONG LOCAL i AS LONG FOR i = 1 TO 3 INCR x TXT.PRINT "Thread x = " & STR$(x) SLEEP 1000 NEXT END FUNCTION FUNCTION PBMAIN() AS LONG LOCAL lDebug AS LONG: TXT.WINDOW EXE.FULL$, 10,10,45,85 TO lDebug LOCAL x, hThread AS LONG TXT.PRINT "MAIN THREAD" THREAD CREATE T1(0) TO hThread DO INCR x TXT.PRINT "Main application x = " & STR$(x) SLEEP 300 LOOP UNTIL THREADCOUNT = 1 TXT.PRINT "Thread has ended" 'Finalise TXT.COLOR = %RGB_BLUE TXT.PRINT TXT.PRINT " ....Press any key to exit": TXT.WAITKEY$: TXT.END END FUNCTION '
Comment
-
-
Originally posted by Mannish Bhandari View PostHow do I check the status of a thread function, check whether it has been completed or not ?
THREAD STATUS statement
THREAD CLOSE statement
THREADCOUNT function
Alos see post #4:
LOOP UNTIL THREADCOUNT = 1
TXT.PRINT "Thread has ended"
Comment
-
-
How do I check the status of a thread function, check whether it has been completed or not ?
Code:#UNIQUE VAR ON FUNCTION PBMAIN LOCAL hDlg,hThread AS DWORD DIALOG NEW 0, "TEST", , , 180, 30, %WS_OVERLAPPEDWINDOW TO hDlg DIALOG SHOW MODELESS hDlg, CALL MyCallBack THREAD CREATE PrintThread(hDlg) TO hThread THREAD CLOSE hThread TO hThread DO DIALOG DOEVENTS LOOP WHILE ISWIN(hDlg) OR THREADCOUNT > 1 END FUNCTION THREAD FUNCTION PrintThread(BYVAL hDlg AS DWORD) AS LONG LOCAL pagenum AS LONG FOR pagenum = 1 TO 100 DIALOG SET TEXT hDlg,USING$("Printed page #",pagenum) IF ISFALSE(ISWIN(hDlg)) THEN BEEP:EXIT FOR SLEEP 50 NEXT END FUNCTION CALLBACK FUNCTION MyCallBack() SELECT CASE AS LONG CB.MSG CASE %WM_INITDIALOG CASE %WM_CLOSE END SELECT END FUNCTION
Comment
-
-
A thread handle is automatically (by Windows) a synchronization object which can be used in a WaitForSingleObject() or [Message]WaitForMultipleObjects() WinAPI function. The handle is signalled ("satisfies the Wait") when the thread function completes.
See: https://learn.microsoft.com/en-us/wi...wait-functions.
THREAD STATUS is deficient in that fact it can't handle a zero return code from the thread function (means the call failed see PB doc). But you can avoid that issue by THINKING about your exit code BEFORE you write it.
The "hThread" value returned by PB THREAD CREATE is in fact a Windows handle, so it may be used with the WinAPI Wait Functions. I have multiple examples of this in the Source Code Forum, e.g., GUI + Worker Thread + Abort Demo 11-24-07 (DDT syntax)
Last edited by Michael Mattias; 5 May 2023, 12:06 PM.Michael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
Comment
-
Comment