A thread created with THREAD CREATE will automatically terminate when its parent process terminates, e.g., a dialog even though the thread has not completed it's task?
Announcement
Collapse
No announcement yet.
Is This Correct?
Collapse
X
-
Mike, that's what I thought; however, I think it depends on where the thread(s) w/as created. For example:
PBMAIN/WINMAIN creates window(s)
window calls intermediate process
intermediate process creates thread(s)
If the intermediate process has no way of knowing that the window has closed it will continue running as will the thread(s). Could produce a gpf somewhere along the line.Walt Decker
Comment
-
If the intermediate process has no way of knowing that the window has closed it will continue running as will the thread(s). Could produce a gpf somewhere along the line
Threads are owned by processes. When the process dies, any owned threads are terminated, too... as shown in the doc I found in the post linked in this forum thread.
What happens in one process never affects what happens in threads owned by another process (unless you do some VERY deliberate things).
Attempts to access memory "owned" by a non-existent process give the same results as any other attempt to use unowned memory.... a protection fault with reason code 0x00000005.
MCMMichael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
Comment
-
I just created a program where threads create threads and when
the main thread terminates they all go bye bye. I also say, huh.
If a second thread creates threads, the created threads are not terminated when the second thread finishes.Last edited by Mike Doty; 26 Feb 2009, 12:23 PM.The world is full of apathy, but who cares?
Comment
-
just created a program where threads create threads and when the main thread terminates they all go bye bye.
If a second thread creates threads, the created threads are not terminated when the second thread finishes
Threads do not own threads, processes own threads.Michael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
Comment
-
>Testing is good
I guess I just could not believe it: someone was in doubt, so they actually tried it to see what happens.
I can leave this world now, for I have been to the mountaintop, and have seen the promised land.Michael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
Comment
-
Yes, it does:
Code:#PBFORMS CREATED V1.51 '------------------------------------------------------------------------------ ' The first line in this file is a PB/Forms metastatement. ' It should ALWAYS be the first line of the file. Other ' PB/Forms metastatements are placed at the beginning and ' end of "Named Blocks" of code that should be edited ' with PBForms only. Do not manually edit or delete these ' metastatements or PB/Forms will not be able to reread ' the file correctly. See the PB/Forms documentation for ' more information. ' Named blocks begin like this: #PBFORMS BEGIN ... ' Named blocks end like this: #PBFORMS END ... ' Other PB/Forms metastatements such as: ' #PBFORMS DECLARATIONS ' are used by PB/Forms to insert additional code. ' Feel free to make changes anywhere else in the file. '------------------------------------------------------------------------------ #COMPILE EXE #DIM ALL '------------------------------------------------------------------------------ ' ** Includes ** '------------------------------------------------------------------------------ #PBFORMS BEGIN INCLUDES #IF NOT %DEF(%WINAPI) #INCLUDE "WIN32API.INC" #ENDIF #PBFORMS END INCLUDES '------------------------------------------------------------------------------ '------------------------------------------------------------------------------ ' ** Constants ** '------------------------------------------------------------------------------ #PBFORMS BEGIN CONSTANTS %IDD_DIALOG1 = 101 %IDC_BUTTON1 = 102 #PBFORMS END CONSTANTS '------------------------------------------------------------------------------ '------------------------------------------------------------------------------ ' ** Declarations ** '------------------------------------------------------------------------------ DECLARE CALLBACK FUNCTION ShowDIALOG1Proc() DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG DECLARE CALLBACK FUNCTION ShowDIALOG2Proc() DECLARE FUNCTION ShowDIALOG2(BYVAL hParent AS DWORD) AS LONG DECLARE SUB Do_Nothing() DECLARE THREAD FUNCTION A_Thread(BYVAL DWORD) AS LONG #PBFORMS DECLARATIONS '------------------------------------------------------------------------------ '------------------------------------------------------------------------------ ' ** Main Application Entry Point ** '------------------------------------------------------------------------------ FUNCTION PBMAIN() ShowDIALOG1 %HWND_DESKTOP END FUNCTION '------------------------------------------------------------------------------ '------------------------------------------------------------------------------ ' ** CallBacks ** '------------------------------------------------------------------------------ CALLBACK FUNCTION ShowDIALOG1Proc() SELECT CASE AS LONG CBMSG CASE %WM_INITDIALOG ' Initialization handler showdialog2(CBHNDL) CASE %WM_NCACTIVATE STATIC hWndSaveFocus AS DWORD IF ISFALSE CBWPARAM THEN ' Save control focus hWndSaveFocus = GetFocus() ELSEIF hWndSaveFocus THEN ' Restore control focus SetFocus(hWndSaveFocus) hWndSaveFocus = 0 END IF CASE %WM_COMMAND ' Process control notifications SELECT CASE AS LONG CBCTL END SELECT END SELECT END FUNCTION '------------------------------------------------------------------------------ CALLBACK FUNCTION ShowDIALOG2Proc() SELECT CASE AS LONG CBMSG CASE %WM_INITDIALOG ' Initialization handler CASE %WM_NCACTIVATE STATIC hWndSaveFocus AS DWORD IF ISFALSE CBWPARAM THEN ' Save control focus hWndSaveFocus = GetFocus() ELSEIF hWndSaveFocus THEN ' Restore control focus SetFocus(hWndSaveFocus) hWndSaveFocus = 0 END IF CASE %WM_COMMAND ' Process control notifications SELECT CASE AS LONG CBCTL CASE %IDC_BUTTON1 CALL Do_Nothing END SELECT END SELECT END FUNCTION '------------------------------------------------------------------------------ ' ** Dialogs ** '------------------------------------------------------------------------------ FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG LOCAL lRslt AS LONG #PBFORMS BEGIN DIALOG %IDD_DIALOG1->-> LOCAL hDlg AS DWORD DIALOG NEW hParent, "main", 164, 103, 261, 121, %WS_POPUP OR %WS_BORDER OR _ %WS_DLGFRAME OR %WS_CAPTION OR %WS_SYSMENU OR %WS_CLIPSIBLINGS OR _ %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR _ %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR _ %WS_EX_RIGHTSCROLLBAR, TO hDlg #PBFORMS END DIALOG DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt #PBFORMS BEGIN CLEANUP %IDD_DIALOG1 #PBFORMS END CLEANUP FUNCTION = lRslt END FUNCTION '------------------------------------------------------------------------------ FUNCTION ShowDIALOG2(BYVAL hParent AS DWORD) AS LONG LOCAL lRslt AS LONG #PBFORMS BEGIN DIALOG %IDD_DIALOG1->-> LOCAL hDlg AS DWORD DIALOG NEW hParent, "Create thread in other process", 164, 103, 261, 121, %WS_POPUP OR %WS_BORDER OR _ %WS_DLGFRAME OR %WS_CAPTION OR %WS_SYSMENU OR %WS_CLIPSIBLINGS OR _ %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR _ %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR _ %WS_EX_RIGHTSCROLLBAR, TO hDlg CONTROL ADD BUTTON, hDlg, %IDC_BUTTON1, "Create Thread", 65, 75, 65, 20 #PBFORMS END DIALOG DIALOG SHOW MODELESS hDlg, CALL ShowDIALOG2Proc TO lRslt #PBFORMS BEGIN CLEANUP %IDD_DIALOG1 #PBFORMS END CLEANUP FUNCTION = lRslt END FUNCTION SUB Do_Nothing LOCAL I AS LONG LOCAL tHndl AS LONG FOR I = 1 TO 10 THREAD CREATE A_Thread(I) TO tHndl NEXT I SLEEP 0 DO DIALOG DOEVENTS 100 LOOP END SUB THREAD FUNCTION A_Thread(BYVAL Q AS DWORD) AS LONG LOCAL I AS LONG DO INCR I IF I MOD 100000 = 0 THEN ' msgbox "this is thread " + str$(Q) + " at iteration " + str$(I) END IF LOOP MSGBOX "Thread " + STR$(Q) + " Closing" END FUNCTION
Walt Decker
Comment
-
Closing a dialog is not ending the program.
The code locked up my machine. Is your second dialog supposed to be modeless?
Anyway, do you want me to debug it?
The second dialog caption says "Create thread in another process". That is not true.
This is not the correct way to check for a button click:
Code:CASE %WM_COMMAND ' Process control notifications SELECT CASE AS LONG CBCTL CASE %IDC_BUTTON1 IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN 'added this CALL Do_Nothing END IF 'added this END SELECT
Code:DO DIALOG DOEVENTS 100 LOOP
I'd start over on this one.Last edited by Mike Doty; 26 Feb 2009, 03:28 PM.The world is full of apathy, but who cares?
Comment
-
Mike, it did exactly what it's supposed to do, i. e. illustrate a point and clarify a point in my own mind; something that was discussed in the windows programming category concerning whether an application will continue running if all the dialogs are closed.
Yes, the second dialog is supposed to be modeless, and no, there's no need to debug it.
The caption is true. The threads were created in another process.Last edited by Walt Decker; 26 Feb 2009, 03:31 PM.Walt Decker
Comment
-
>Closing a dialog is not ending the program
>...
>There is only 1 process.
I do believe, sir, you have acquired an excellent grasp of these concepts.
I am also pleased you are NOT writing Mr. Decker's code for him, instead guiding him to the correct solution.
This is better than sex.
MCMMichael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
Comment
-
>Well, Mike, your definition of "process" is different than mine
When you are posting in a Windows' programming forum, the meaning of the word "process" is not subject to debate or personal preference.
MCMMichael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
Comment
-
Speaking of PID's:
Did you know you can also suspend/resume a process?
In Task Manager:
Click View
Click Select Columns
Click PID (Process Identifer)
Download:
Wayne Diamond - Suspend/resume a process PID
You can now stop processes using the PID (at your own risk.)The world is full of apathy, but who cares?
Comment
-
A thread created with THREAD CREATE will automatically terminate when its parent process terminates, e.g., a dialog even though the thread has not completed it's task?
originally I set out to show MCM he was wrong, and in turn showed that I was wrong (although I can not explain why larger projects all point to a misuse of threads??? when closing a program
My Example below....and I tried to make it as "Sloppy" as I could, but each test comes back that the process exits
Code:#COMPILE EXE #DIM ALL #INCLUDE "Win32Api.Inc" GLOBAL LiveThread AS LONG DECLARE CALLBACK FUNCTION ShowDIALOG1Proc() DECLARE FUNCTION ThreadFunction(BYVAL X AS LONG) AS LONG DECLARE FUNCTION ThreadFunction2(BYVAL X AS LONG) AS LONG CALLBACK FUNCTION ShowDIALOG1Proc() LOCAL MyThread AS LONG SELECT CASE AS LONG CBMSG CASE %WM_INITDIALOG ' Initialization handler CASE %WM_NCACTIVATE STATIC hWndSaveFocus AS DWORD IF ISFALSE CBWPARAM THEN ' Save control focus hWndSaveFocus = GetFocus() ELSEIF hWndSaveFocus THEN ' Restore control focus SetFocus(hWndSaveFocus) hWndSaveFocus = 0 END IF CASE %WM_COMMAND ' Process control notifications SELECT CASE AS LONG CBCTL CASE 100 THREAD CREATE ThreadFunction(1) TO MyThread IF MyThread THEN LiveThread = %TRUE MSGBOX FUNCNAME$ + $CR + "Thread Created = " + STR$(MyThread) + $CR + "Live Thread = " + STR$(LiveThread) END SELECT END SELECT END FUNCTION FUNCTION PBMAIN () AS LONG LOCAL hDlg AS LONG DIALOG NEW %HWND_DESKTOP, "Test Dialog", 0,0, 100, 100, %WS_SYSMENU TO hDlg CONTROL ADD BUTTON, hDlg, 100, "Set Ok to close", 5, 5, 10, 50 DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc END FUNCTION FUNCTION ThreadFunction(BYVAL X AS LONG) AS LONG LOCAL LoopCount AS LONG LOCAL MyThread2 AS LONG DO LoopCount = LoopCount + 1 IF MyThread2 = 0 THEN THREAD CREATE ThreadFunction2(1) TO MyThread2 LOOP WHILE LiveThread = %TRUE MSGBOX FUNCNAME$ + $CR + "I looped" + STR$(LoopCount) + " times" END FUNCTION FUNCTION ThreadFunction2(BYVAL X AS LONG) AS LONG LOCAL LoopCount AS LONG DO LoopCount = LoopCount + 1 LOOP WHILE LiveThread = %TRUE MSGBOX FUNCNAME$ + $CR + "I looped" + STR$(LoopCount) + " times" END FUNCTION
Engineer's Motto: If it aint broke take it apart and fix it
"If at 1st you don't succeed... call it version 1.0"
"Half of Programming is coding"....."The other 90% is DEBUGGING"
"Document my code????" .... "WHYYY??? do you think they call it CODE? "
Comment
Comment