?? tut16.html
字號:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Author" content="Iczelion">
<meta name="GENERATOR" content="Mozilla/4.51 [en] (Win95; I) [Netscape]">
<title>Iczelion's Win32 Assembly Tutorial 16: Event Object</title>
</head>
<body text="#FFFFFF" bgcolor="#000080" link="#FFFF00" vlink="#8080FF" alink="#FF00FF">
<center>
<h1>
<font face="Arial,Helvetica"><font color="#FFFFCC">Tutorial 16: Event Object</font></font></h1></center>
<font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>We will
learn what an event object is and how to use it in a multithreaded program.</font></font></font><font face="Arial,Helvetica"><font size=-1></font></font>
<p><font face="Arial,Helvetica"><font size=-1><font color="#FFFFFF">Download
the example </font><font color="#000000"><a href="files/tut16.zip">here</a>.</font></font></font>
<h3>
<font face="Arial,Helvetica"><font color="#33FF33"><font size=+0>Theory:</font></font></font></h3>
<font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>From the
previous tutorial, I demonstrated how threads communicate with a custom
window message. I left out two other methods: global variable and event
object. We will use both of them in this tutorial.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>An
event object is like a switch: it has only two states: on or off. When
an event object is turned on, it's in the "signalled" state. When it is
turned off, it's in the "nonsignalled" state. You create an event object
and put in a code snippet in the relevant threads to watch for the state
of the event object. If the event object is in the nonsignalled state,
the threads that wait for it will be asleep.When the threads are in wait
state, they consume little CPU time.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>You
create an event object by calling CreateEvent function which has the following
syntax:</font></font></font><font face="Arial,Helvetica"><font size=-1></font></font>
<p><b><font face="Arial,Helvetica"><font color="#FFFFCC"><font size=-1>CreateEvent
proto lpEventAttributes:DWORD,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFCC"><font size=-1>
bManualReset:DWORD,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFCC"><font size=-1>
bInitialState:DWORD,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFCC"><font size=-1>
lpName:DWORD</font></font></font></b><font face="Arial,Helvetica"><font size=-1></font></font>
<p><font face="Arial,Helvetica"><font size=-1><b><font color="#009900">lpEventAttribute</font></b><font color="#FFFFFF">-->
If you specify NULL value, the event object is created with default security
descriptor.</font></font></font>
<br><font face="Arial,Helvetica"><font size=-1><b><font color="#009900">bManualReset</font></b><font color="#FFFFFF">-->
If you want Windows to automatically reset the event object to nonsignalled
state after WaitForSingleObject call, you must specify FALSE as this parameter.
Else you must manually reset the event object with the call to ResetEvent.</font></font></font>
<br><font face="Arial,Helvetica"><font size=-1><b><font color="#009900">bInitialState</font></b><font color="#FFFFFF">-->
If you want the event object to be created in the signalled state, specify
TRUE as this parameter else the event object will be created in the nonsignalled
state.</font></font></font>
<br><font face="Arial,Helvetica"><font size=-1><b><font color="#009900">lpName</font></b><font color="#000000">
</font><font color="#FFFFFF">--> Pointer to an ASCIIZ string that is the
name of the event object. This name is used when you want to call OpenEvent.</font></font></font><font face="Arial,Helvetica"><font size=-1></font></font>
<p><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>If
the call is successful, it returns the handle to the newly created event
object else it returns NULL.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>You
can modify the state of an event object with two API calls: SetEvent and
ResetEvent. SetEvent function sets the event object into signalled state.
ResetEvent does the reverse.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>When
the event object is created, you must put the call to WaitForSingleObject
in the thread that wants to watch for the state of the event object. WaitForSingleObject
has the following syntax:</font></font></font><font face="Arial,Helvetica"><font size=-1></font></font>
<p><b><font face="Arial,Helvetica"><font color="#FFFFCC"><font size=-1>WaitForSingleObject
proto hObject:DWORD, dwTimeout:DWORD</font></font></font></b><font face="Arial,Helvetica"><font size=-1></font></font>
<p><font face="Arial,Helvetica"><font size=-1><b><font color="#009900">hObject</font></b><font color="#FFFFFF">
--> A handle to one of the synchronization object. Event object is a type
of synchronization object.</font></font></font>
<br><font face="Arial,Helvetica"><font size=-1><b><font color="#009900">dwTimeout
</font></b><font color="#FFFFFF">--> specify the time in milliseconds that
this function will wait for the object to be in signalled state. If the
specified time has passed and the event object is still in nonsignalled
state, WaitForSingleObject returns the the caller. If you want to wait
for the object indefinitely, you must specify the value INFINITE as this
parameter.</font></font></font>
<h3>
<font face="Arial,Helvetica"><font color="#33FF33"><font size=+0>Example:</font></font></font></h3>
<font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>The example
below displays a window waiting for the user to select a command from the
menu. If the user selects "run thread", the thread starts the savage calculation.
When it's finished, a message box appears informing the user that the job
is done. During the time that the thread is running, the user can select
"stop thread" to stop the thread.</font></font></font><font face="Arial,Helvetica"><font size=-1></font></font>
<p><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>.386</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>.model
flat,stdcall</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>option
casemap:none</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>WinMain
proto :DWORD,:DWORD,:DWORD,:DWORD</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>include
\masm32\include\windows.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>include
\masm32\include\user32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>include
\masm32\include\kernel32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>includelib
\masm32\lib\user32.lib</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>includelib
\masm32\lib\kernel32.lib</font></font></font></b><font face="Arial,Helvetica"><font size=-1></font></font>
<p><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>.const</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#66FF99"><font size=-1>IDM_START_THREAD
equ 1</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#66FF99"><font size=-1>IDM_STOP_THREAD
equ 2</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>IDM_EXIT
equ 3</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>WM_FINISH
equ WM_USER+100h</font></font></font></b><font face="Arial,Helvetica"><font size=-1></font></font>
<p><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>.data</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>ClassName
db "Win32ASMEventClass",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>AppName
db "Win32 ASM Event Example",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>MenuName
db "FirstMenu",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#66FF99"><font size=-1>SuccessString
db "The calculation is completed!",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#66FF99"><font size=-1>StopString
db "The thread is stopped",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#66FF99"><font size=-1>EventStop
BOOL FALSE</font></font></font></b><font face="Arial,Helvetica"><font size=-1></font></font>
<p><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>.data?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>hInstance
HINSTANCE ?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>CommandLine
LPSTR ?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>hwnd
HANDLE ?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>hMenu
HANDLE ?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFF99"><font size=-1>ThreadID
DWORD ?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#66FF99"><font size=-1>ExitCode
DWORD ?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#66FF99"><font size=-1>hEventStart
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -