flat assembler
Message board for the users of flat assembler.

Index > Windows > How to stop infinite looping thread?

Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Hello everyone, I'm trying to stop infinite looping thread but I can't.. Can someone help me ? Here's skeleton..
Code:
hThread dd ?
invoke CreateThread(...thread1..,hThread)
invoke Sleep,5000
invoke TerminateThread,[hThread],0
ret
..
proc thread1
@@:
do something
jmp @b
ret
endp    

Thank you.
Post 18 Oct 2011, 20:36
View user's profile Send private message Reply with quote
Akujin



Joined: 11 Oct 2011
Posts: 26
Location: Exile
Akujin
Maybe...
Code:
hThread dd ?
threadID dd ?
invoke CreateThread(...thread1..,threadID)
mov [hThread],eax
invoke Sleep,5000
invoke TerminateThread,[hThread],0
ret
..
proc thread1
@@:
do something
jmp @b
ret
endp
    

_________________
CLI
HLT
Post 18 Oct 2011, 20:46
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Oh! then what this does ?!
lpThreadId [out, optional]
A pointer to a variable that receives the thread identifier. If this parameter is NULL, the thread identifier is not returned.

what's difference between them ? can anyone explain ?
Post 18 Oct 2011, 22:00
View user's profile Send private message Reply with quote
Akujin



Joined: 11 Oct 2011
Posts: 26
Location: Exile
Akujin
"Until the thread terminates, the thread identifier uniquely identifies the thread throughout the system."
Post 18 Oct 2011, 22:33
View user's profile Send private message Visit poster's website Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji

shut down the power supply ?
(I tried, it works) Wink

a hammer blow on the processor, it works too.

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 19 Oct 2011, 00:34
View user's profile Send private message Send e-mail Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1409
Location: Toronto, Canada
AsmGuru62
The proper way to do it is to have the thread do some work and then check the global variable which will be 1 to continue looping and 0 to stop looping.
The proper way to stop thread is set the variable to 0 and let thread to RETURN from its THREAD PROCEDURE.
Using TerminateThread may cause instability in KERNEL32.DLL - depending of what thread will be doing.
The variable must be 32-bit in size and must be aligned on 4 byte boundary to allow for atomic read/write operations.
Post 19 Oct 2011, 14:16
View user's profile Send private message Send e-mail Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
Use WaitMessage or one of the xxxWaitForxxx API to pause the thread. In the message case you can use Peek/GetMessage to read the message and choose what to do. Messages can be sent to the thread using PostThreadMessage.
Post 20 Oct 2011, 17:21
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Overflowz wrote:
Oh! then what this does ?!
lpThreadId [out, optional]
A pointer to a variable that receives the thread identifier. If this parameter is NULL, the thread identifier is not returned.

what's difference between them ? can anyone explain ?
One is an id, the other is a handle. When you need to "take action on something", you generally need a handle in Windows. You can OpenThread on the id to get a handle. Remember to CloseHandle, since kernel-mode structures aren't deallocated until you close the handle (or your process terminates).

And, as others have suggested, TerminateThread isn't good and should only be used as a last resort.

_________________
Image - carpe noctem
Post 20 Oct 2011, 17:36
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Thank you everyone, I understand it now. Smile
Post 20 Oct 2011, 21:01
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.