The MASM Forum Archive 2004 to 2012

Project Support Forums => GoAsm Assembler and Tools => Topic started by: Dogim on November 17, 2010, 08:58:51 PM

Title: After the Thread execute the program does not exit as it should ?
Post by: Dogim on November 17, 2010, 08:58:51 PM
Hallo to all and goodnight.
I,m having trouble figuring this one out.
Its  iczelion multithread tutorial number 15 to be exact( attachment included).
I,m not  a champion debugger, but for what i could figure out ,is that  MessageLoop:
    invoke GetMessage,addr msg,0,0,0 ; Retrieves a message from the calling thread's message queue
    or eax,eax
    jz >ExitProgram
    invoke TranslateMessage,addr msg
    invoke DispatchMessage, addr  msg
    jmp <MessageLoop
ExitProgram:
int 3 ; For testing purpose only
    invoke ExitProcess,[msg.wParam]

it looks like the debugger never reaches the int3 instruction and also it never reaches a breakpoint before the exitcode ( on the push ), or on the exitcode api call itself.
I,m missing something, but i can't put my finger on it.
The Thread itself exit with non zero.
Help is much appreciated.
Thank you.

Title: Re: After the Thread execute the program does not exit as it should ?
Post by: donkey on November 17, 2010, 09:46:43 PM
Executes fine here Vista x86-32
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Antariy on November 17, 2010, 09:56:35 PM
Quote from: Dogim on November 17, 2010, 08:58:51 PM

MessageLoop:
    invoke GetMessage,addr msg,0,0,0 ; Retrieves a message from the calling thread's message queue
    or eax,eax
    jz >ExitProgram
    invoke TranslateMessage,addr msg
    invoke DispatchMessage, addr  msg
    jmp <MessageLoop
ExitProgram:
int 3 ; For testing purpose only
    invoke ExitProcess,[msg.wParam]

it looks like the debugger never reaches the int3 instruction and also it never reaches a breakpoint before the exitcode ( on the push ), or on the exitcode api call itself.
I,m missing something, but i can't put my finger on it.
The Thread itself exit with non zero.
Help is much appreciated.
Thank you.

What if change the piece of message loop to:

invoke GetMessage,addr msg,0,0,0 ; Retrieves a message from the calling thread's message queue
    or eax,eax
    jle >ExitProgram <--- CHANGED THIS


This is not help?
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Dogim on November 17, 2010, 10:11:47 PM
Quote from: donkey on November 17, 2010, 09:46:43 PM
Executes fine here Vista x86-32
Hi Donkey, it does execute on my Win 7 64 Bits OS, but the primary Thread  does not exit, so in Rad when  try to recompile it, i get can't delete error, a Ctrl Alt Del shows it still running in the background.
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: donkey on November 17, 2010, 10:14:57 PM
Hi Dogim,

I spoke too soon, the problem with the exit is only present when you use the menu to exit, I was using the close button. You should post a WM_CLOSE message instead of DestroyWindow, DestoryWindow will flush the message queue so you will not receive the NULL return from GetMessage (as far as I have been able to determine).

.kill_and_exit
   invoke PostMessage,WM_CLOSE,0,0
   jmp >.defwindowproc
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Antariy on November 17, 2010, 10:16:51 PM
So, my suggestion about using of "jle >ExitProgram" is fully ignored :P
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Dogim on November 17, 2010, 10:20:23 PM
PROBLEM  SOLVED fixed, Thanks Donkey and Antariy   for  the help.
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Dogim on November 17, 2010, 10:21:38 PM
Quote from: Antariy on November 17, 2010, 10:16:51 PM
So, my suggestion about using of "jle >ExitProgram" is fully ignored :P

No i did not ignore your fix, but Donkey was so fast with his answer , i didn't get the chance to reply to your answer  :U :bg
I used both fixes  :lol
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Antariy on November 17, 2010, 10:26:59 PM
Quote from: Dogim on November 17, 2010, 10:21:38 PM
Quote from: Antariy on November 17, 2010, 10:16:51 PM
So, my suggestion about using of "jle >ExitProgram" is fully ignored :P

No i did not ignore your fix, but Donkey was so fast with his answer , i didn't get the chance to reply to your answer  :U :bg
I used both fixes  :lol

Just using of jle is as rule - if you destroy window, then GetMessage return value less than 0. If WM_QUIT is received, then GetMessage return 0 - JLE check for zero or negative value  :U
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Dogim on November 17, 2010, 10:41:53 PM
Quote from: Antariy on November 17, 2010, 10:26:59 PM
Quote from: Dogim on November 17, 2010, 10:21:38 PM
Quote from: Antariy on November 17, 2010, 10:16:51 PM
So, my suggestion about using of "jle >ExitProgram" is fully ignored :P

No i did not ignore your fix, but Donkey was so fast with his answer , i didn't get the chance to reply to your answer  :U :bg
I used both fixes  :lol

Just using of jle is as rule - if you destroy window, then GetMessage return value less than 0. If WM_QUIT is received, then GetMessage return 0 - JLE check for zero or negative value  :U

Hi  Antariy , the jle>ExitProgram  in combination with Destroywindow function, still keeps it running in the background, when i exit using the menu,exit true the menu only works when i send a WM_CLOSE message as Donkey stated in his answer.
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Antariy on November 17, 2010, 10:48:45 PM
Quote from: Dogim on November 17, 2010, 10:41:53 PM
Hi  Antariy , the jle>ExitProgram  in combination with Destroywindow function, still keeps it running in the background, when i exit using the menu,exit true the menu only works when i send a WM_CLOSE message as Donkey stated in his answer.

Did not you call PostQuitMessage or SendMessage,hwnd,WM_QUIT,0,0 before DestroyWindow? That must be at this manner.
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Dogim on November 17, 2010, 10:59:21 PM
Quote
Did not you call PostQuitMessage or SendMessage,hwnd,WM_QUIT,0,0 before DestroyWindow? That must be at this manner.
.wmfinish
cmp D[uMsg],WM_FINISH
jne >>.defwindowproc
invoke MessageBoxEx,NULL,ADDR szMbString,ADDR szAppName,MB_OK,0
;jmp >.exit -->FIXED
jmp >.defwindowproc

.kill_and_exit
: invoke DestroyWindow,[hWnd]
: jmp >.exit
invoke PostMessage,[hWnd],WM_CLOSE,0,0 -->FIXED
        jmp >.defwindowproc   ---FIXED

The above code works with jle > ExitProgram, I called the destroywindow api at first, but  i discovered that when i processed the WM_FINISH message i was jumping to >.exit, i should continue to DefWinProc here i think.But however, this way the programs exit true the close and also true the menu.
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Antariy on November 17, 2010, 11:12:39 PM
Quote from: Dogim on November 17, 2010, 10:59:21 PM
Quote
Did not you call PostQuitMessage or SendMessage,hwnd,WM_QUIT,0,0 before DestroyWindow? That must be at this manner.
.wmfinish
cmp D[uMsg],WM_FINISH
jne >>.defwindowproc
invoke MessageBoxEx,NULL,ADDR szMbString,ADDR szAppName,MB_OK,0
;jmp >.exit -->FIXED
jmp >.defwindowproc

.kill_and_exit
: invoke DestroyWindow,[hWnd]
: jmp >.exit
invoke PostMessage,[hWnd],WM_CLOSE,0,0 -->FIXED
        jmp >.defwindowproc   ---FIXED

The above code works with jle > ExitProgram, I called the destroywindow api at first, but  i discovered that when i processed the WM_FINISH message i was jumping to >.exit, i should continue to DefWinProc here i think.But however, this way the programs exit true the close and also true the menu.

Usual sequence is PostQuitMessage (or sending of WM_QUIT), and after that - DestroyWindow.

You can make reaction for WM_FINISH like this:

invoke SendMessage,[hWnd],WM_CLOSE,0,0
ret


Or something like - sorry, I'm not very familar with GoAsm yet.
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Dogim on November 17, 2010, 11:27:02 PM
Quote from: Antariy on November 17, 2010, 11:12:39 PM

Usual sequence is PostQuitMessage (or sending of WM_QUIT), and after that - DestroyWindow.

You can make reaction for WM_FINISH like this:

invoke SendMessage,[hWnd],WM_CLOSE,0,0
ret


Or something like - sorry, I'm not very familar with GoAsm yet.

This Will shutdown the whole program as soon as the secondary Thread stops executing, so if you want to restart the secondary thread again, your gonna have to start the whole program again.
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Antariy on November 17, 2010, 11:34:46 PM
Quote from: Dogim on November 17, 2010, 11:27:02 PM
Quote from: Antariy on November 17, 2010, 11:12:39 PM

Usual sequence is PostQuitMessage (or sending of WM_QUIT), and after that - DestroyWindow.

You can make reaction for WM_FINISH like this:

invoke SendMessage,[hWnd],WM_CLOSE,0,0
ret


Or something like - sorry, I'm not very familar with GoAsm yet.

This Will shutdown the whole program as soon as the secondary Thread stops executing, so if you want to restart the secondary thread again, your gonna have to start the whole program again.

Well, I'm not dig into the program itself. I just suggest a sequence of things to close window, you can adapt anything for your needs  :U
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Dogim on November 17, 2010, 11:38:39 PM
Quote from: Antariy on November 17, 2010, 11:34:46 PM
Quote from: Dogim on November 17, 2010, 11:27:02 PM
Quote from: Antariy on November 17, 2010, 11:12:39 PM

Usual sequence is PostQuitMessage (or sending of WM_QUIT), and after that - DestroyWindow.

You can make reaction for WM_FINISH like this:

invoke SendMessage,[hWnd],WM_CLOSE,0,0
ret


Or something like - sorry, I'm not very familar with GoAsm yet.

This Will shutdown the whole program as soon as the secondary Thread stops executing, so if you want to restart the secondary thread again, your gonna have to start the whole program again.

Well, I'm not dig into the program itself. I just suggest a sequence of things to close window, you can adapt anything for your needs  :U

Thanks for all the tips, it does work however, so ill use all in my further programming journey  :U

Title: Re: After the Thread execute the program does not exit as it should ?
Post by: donkey on November 17, 2010, 11:48:54 PM
Hi Dogim,

Some unsolicited advice:

NEVER use SendMessage or PostMessage to send a WM_QUIT message, the only reliable way to use that message is PostQuitMessage. When sending the WM_CLOSE message to your own application be sure to use PostMessage, SendMessage will wait for it to return and though it does not currently present a problem it may on future versions of Windows if the window destructor sequence is changed. I usually use WM_CLOSE as it allows my program to process all unfinished business and free resources within a single message handler, PostQuitMessage can be executed from the WM_CLOSE handler so that the Close button and the Exit menu item will execute the same wrap up code. Also I found out early that using PostQuitMessage all over the program can complicate debugging on larger programs, a single call in the WM_CLOSE handler is much easier to deal with.

Edgar
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Antariy on November 18, 2010, 12:00:16 AM
Quote from: donkey on November 17, 2010, 11:48:54 PM
Hi Dogim,

Some unsolicited advice:

NEVER use SendMessage or PostMessage to send a WM_QUIT message, the only reliable way to use that message is PostQuitMessage. When sending the WM_CLOSE message to your own application be sure to use PostMessage, SendMessage will wait for it to return and though it does not currently present a problem it may on future versions of Windows if the window destructor sequence is changed. I usually use WM_CLOSE as it allows my program to process all unfinished business and free resources within a single message handler, PostQuitMessage can be executed from the WM_CLOSE handler so that the Close button and the Exit menu item will execute the same wrap up code. Also I found out early that using PostQuitMessage all over the program can complicate debugging on larger programs, a single call in the WM_CLOSE handler is much easier to deal with.

Edgar

Yes, right, using of SendMessage is not proper. I'm too tired. But with using of PostMessage,WM_QUIT... I never had a problems, really.
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: donkey on November 18, 2010, 12:07:26 AM
Quote from: Antariy on November 18, 2010, 12:00:16 AM
Yes, right, using of SendMessage is not proper. I'm too tired. But with using of PostMessage,WM_QUIT... I never had a problems, really.


Perhaps you got lucky, I am looking for an old Win98SE program I had that BSODed when it was sent without PostQuitMessage, something in the window procedure that the program was doing caused a lock-up.

Quote from: MSDNRemarks

The WM_QUIT message is not associated with a window and therefore will never be received through a window's window procedure. It is retrieved only by the GetMessage or PeekMessage functions.

Do not post the WM_QUIT message using the PostMessage function; use PostQuitMessage.

Edgar
Title: Re: After the Thread execute the program does not exit as it should ?
Post by: Dogim on November 18, 2010, 09:44:30 AM
Quote from: donkey on November 18, 2010, 12:07:26 AM
Quote from: Antariy on November 18, 2010, 12:00:16 AM
Yes, right, using of SendMessage is not proper. I'm too tired. But with using of PostMessage,WM_QUIT... I never had a problems, really.


Perhaps you got lucky, I am looking for an old Win98SE program I had that BSODed when it was sent without PostQuitMessage, something in the window procedure that the program was doing caused a lock-up.

Quote from: MSDNRemarks

The WM_QUIT message is not associated with a window and therefore will never be received through a window's window procedure. It is retrieved only by the GetMessage or PeekMessage functions.

Do not post the WM_QUIT message using the PostMessage function; use PostQuitMessage.

Edgar
Thanks Donkey for the tip. :U