?? lion-tutorial16.htm
字號(hào):
<b>WinMain endp</b>
<p><b>WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM</b> <br>
<b> .IF uMsg==WM_CREATE</b> <br>
<b> invoke CreateEvent,NULL,FALSE,FALSE,NULL</b>
<br>
<b> mov hEventStart,eax</b>
<br>
<b> mov eax,OFFSET ThreadProc</b>
<br>
<b> invoke CreateThread,NULL,NULL,eax,\</b>
<br>
<b>
NULL,0,\</b> <br>
<b>
ADDR ThreadID</b> <br>
<b> invoke CloseHandle,eax</b> <br>
<b> .ELSEIF uMsg==WM_DESTROY</b> <br>
<b> invoke PostQuitMessage,NULL</b>
<br>
<b> .ELSEIF uMsg==WM_COMMAND</b> <br>
<b> mov eax,wParam</b> <br>
<b> .if lParam==0</b> <br>
<b> .if ax==IDM_START_THREAD</b>
<br>
<b>
invoke SetEvent,hEventStart</b> <br>
<b>
invoke EnableMenuItem,hMenu,IDM_START_THREAD,MF_GRAYED</b> <br>
<b>
invoke EnableMenuItem,hMenu,IDM_STOP_THREAD,MF_ENABLED</b> <br>
<b> .elseif
ax==IDM_STOP_THREAD</b> <br>
<b>
mov EventStop,TRUE</b> <br>
<b>
invoke EnableMenuItem,hMenu,IDM_START_THREAD,MF_ENABLED</b> <br>
<b>
invoke EnableMenuItem,hMenu,IDM_STOP_THREAD,MF_GRAYED</b> <br>
<b> .else</b>
<br>
<b>
invoke DestroyWindow,hWnd</b> <br>
<b> .endif</b>
<br>
<b> .endif</b> <br>
<b> .ELSEIF uMsg==WM_FINISH</b> <br>
<b> invoke MessageBox,NULL,ADDR SuccessString,ADDR
AppName,MB_OK</b> <br>
<b> .ELSE</b> <br>
<b> invoke DefWindowProc,hWnd,uMsg,wParam,lParam</b>
<br>
<b> ret</b> <br>
<b>.ENDIF</b> <br>
<b> xor eax,eax</b> <br>
<b> ret</b> <br>
<b>WndProc endp</b>
<p><b>ThreadProc PROC USES ecx Param:DWORD</b> <br>
<b> invoke WaitForSingleObject,hEventStart,INFINITE</b>
<br>
<b> mov ecx,600000000</b> <br>
<b> .WHILE ecx!=0</b> <br>
<b>
.if EventStop!=TRUE</b> <br>
<b>
add eax,eax</b> <br>
<b>
dec ecx</b> <br>
<b>
.else</b> <br>
<b>
invoke MessageBox,hwnd,ADDR StopString,ADDR AppName,MB_OK</b> <br>
<b>
mov EventStop,FALSE</b> <br>
<b>
jmp ThreadProc</b> <br>
<b>
.endif</b> <br>
<b> .ENDW</b> <br>
<b> invoke PostMessage,hwnd,WM_FINISH,NULL,NULL</b>
<br>
<b> invoke EnableMenuItem,hMenu,IDM_START_THREAD,MF_ENABLED</b>
<br>
<b> invoke EnableMenuItem,hMenu,IDM_STOP_THREAD,MF_GRAYED</b>
<br>
<b> jmp ThreadProc</b>
<br>
<b> ret</b> <br>
<b>ThreadProc ENDP</b> <br>
<b>end start</b>
<h3> <b>Analysis:</b></h3>
In this example, I demonstrate another thread technique.
<p><b> .IF uMsg==WM_CREATE</b> <br>
<b> invoke CreateEvent,NULL,FALSE,FALSE,NULL</b>
<br>
<b> mov hEventStart,eax</b>
<br>
<b> mov eax,OFFSET ThreadProc</b>
<br>
<b> invoke CreateThread,NULL,NULL,eax,\</b>
<br>
<b>
NULL,0,\</b> <br>
<b>
ADDR ThreadID</b> <br>
<b> invoke CloseHandle,eax</b>
<p>You can see that I create the event object and the thread during the processing
of WM_CREATE message. I create the event object in the nonsignalled state with
automatic reset. After the event object is created, I create the thread. However
the thread doesn't run immediately because it waits for the event object to
be in the signalled state as the code below:
<p><b>ThreadProc PROC USES ecx Param:DWORD</b> <br>
<b> invoke WaitForSingleObject,hEventStart,INFINITE</b>
<br>
<b> mov ecx,600000000</b>
<p>The first line of the thread procedure is the call to WaitForSingleObject.
It waits infinitely for the signalled state of the event object before it returns.
This means that even when the thread is created, we put it into a dormant state.
<br>
When the user selects "run thread" command from the menu, we set the event object
into signalled state as below:
<p><b> .if ax==IDM_START_THREAD</b>
<br>
<b>
invoke SetEvent,hEventStart</b>
<p>The call to SetEvent turns the event object into the signalled state which
in turn makes the WaitForSingleObject call in the thread procedure return and
the thread starts running. When the user selects "stop thread" command,
we set the value of the global variable "EventStop" to TRUE.
<p><b>
.if EventStop==FALSE</b> <br>
<b>
add eax,eax</b> <br>
<b>
dec ecx</b> <br>
<b>
.else</b> <br>
<b>
invoke MessageBox,hwnd,ADDR StopString,ADDR AppName,MB_OK</b> <br>
<b>
mov EventStop,FALSE</b> <br>
<b>
jmp ThreadProc</b> <br>
<b>
.endif</b>
<p>This stops the thread and jumps to the WaitForSingleObject call again. Note
that we don't have to manually reset the event object into nonsignalled state
because we specify the bManualReset parameter of the CreateEvent call as FALSE.
<hr size="1">
<div align="center"> This article come from Iczelion's asm page, Welcom to <a href="http://asm.yeah.net">http://asm.yeah.net</a></div>
</body>
</html>
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -