📄 c++ builder 程序设计实用教程 - 9_3_2 进程应用方法 - 张晋 - 17xie.htm
字号:
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">单元中定义的一个标准例程</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">BeginThread</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。此例程完整封装了</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">Win32 </FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">CreateThread </FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数,是一个带有异常处理的标准</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">Pascal</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数,几乎可以处理所有自身的异常,相对于使用</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">Win32</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">CreateThread </FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数,其安全系数大大增强。</SPAN></P>
<P class=MsoNormalIndent style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT
face="Times New Roman">BeginThread </FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数在创建时,不是如</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">CreateThread </FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数仅仅完成两项任务:创建一个</SPAN><FONT
face="Times New Roman"> </FONT><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">进程;创建一个能作为进程入口的函数。还增加了几项保护措施:</SPAN><FONT
face="Times New Roman"> </FONT></P>
<P class=MsoNormalIndent style="MARGIN: 0cm 0cm 0pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">把</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">System </FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">单元中声明的全局变量</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">IsMultiThread </FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设为</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">TRUE</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,</SPAN><FONT
face="Times New Roman"> </FONT><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这样</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">CBuilder 5</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的堆栈管理器就知道当前有多个进程在运行,从而防止多个进程同时修改它的内部结构;</SPAN></P>
<P class=MsoNormalIndent style="MARGIN: 0cm 0cm 0pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">另外,在调用</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">BeginThread </FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数时,可创建一个异常框架,允许系统缺省的异常处理句柄捕获任何未有被处理的异常进程。如果在进程函数中有任何未被处理的异常,会自动产生一个退出代码,或者进程返回的句柄为</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">0</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,表示进程没有创建成功,则应用程序将会调用</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">EndThread</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">过程(</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">Procedure
EndThread(ExitCiode:Integer)</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">),自动终止进程的运行。</SPAN></P>
<P class=MsoNormalIndent style="MARGIN: 0cm 0cm 0pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">其完整声明如下:</SPAN></P>
<P class=a style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">function<SPAN
style="mso-spacerun: yes"> </SPAN>BeginThread( SecurityAttributes:
Pointer</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">;</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"> <o:p></o:p></SPAN></FONT></P>
<P class=a style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>StackSize: Integer</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">;</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></FONT></P>
<P class=a style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>ThreadFunc: TThreadFunc</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">;</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"> <o:p></o:p></SPAN></FONT></P>
<P class=a style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>Parameter: Pointer</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">;</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></FONT></P>
<P class=a style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>CreationFlags: Integer</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">;</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></FONT></P>
<P class=a style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>var ThreadId: Integer): Integer</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">;</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormalIndent style="MARGIN: 0cm 0cm 0pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">各参数的使用特点类似</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">CreateThread</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数:</SPAN></P>
<P class=MsoNormalIndent
style="MARGIN: 0cm 0cm 0pt 42.5pt; TEXT-INDENT: -21.25pt; mso-list: l0 level1 lfo1; tab-stops: list 42.25pt"><SPAN
lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN lang=EN-US><FONT
face="Times New Roman">SecurityAttributes</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">参数是一个指向</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">SECURITY_ATTRIBUTES</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">结构的指针,其目的用于设置进程的访问权限,</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">nil</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">表示为默认的安全属性;</SPAN></P>
<P class=MsoNormalIndent
style="MARGIN: 0cm 0cm 0pt 42.5pt; TEXT-INDENT: -21.25pt; mso-list: l0 level1 lfo1; tab-stops: list 42.25pt"><SPAN
lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN lang=EN-US><FONT
face="Times New Roman">StackSize</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">参数用于设置分配给进程的栈空间大小</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">0</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">表示用默认值;</SPAN></P>
<P class=MsoNormalIndent
style="MARGIN: 0cm 0cm 0pt 42.5pt; TEXT-INDENT: -21.25pt; mso-list: l0 level1 lfo1; tab-stops: list 42.25pt"><SPAN
lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN lang=EN-US><FONT
face="Times New Roman">ThreadFunc</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">用于指定一个函数,该函数在进程创建后开始执行代码时调用;</SPAN></P>
<P class=MsoNormalIndent
style="MARGIN: 0cm 0cm 0pt 42.5pt; TEXT-INDENT: -21.25pt; mso-list: l0 level1 lfo1; tab-stops: list 42.25pt"><SPAN
lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN lang=EN-US><FONT
face="Times New Roman">Parameter</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">参数传递给</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">ThreadFunc</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">参数所指定的函数,常为</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">nil</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,或者设为一个</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">32</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">位的指针,指向一个数据结构;</SPAN></P>
<P class=MsoNormalIndent
style="MARGIN: 0cm 0cm 0pt 42.5pt; TEXT-INDENT: -21.25pt; mso-list: l0 level1 lfo1; tab-stops: list 42.25pt"><SPAN
lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
style="FONT: 7pt 'Times New Roman'">
</SPAN></SPAN></SPAN><SPAN lang=EN-US><FONT
face="Times New Roman">CreationFlags</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">参数用于指定进程创建后是不是立即执行,</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">0</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">表示立即执行,</SPAN><SPAN
lang=EN-US><FONT face="Times New Roman">CREATE_SUSPENDED</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">表示处于挂起状态;</SPAN></P>
<P class=MsoNormalIndent
style="MARGIN: 0cm 0cm 0pt 42.5pt; TEXT-INDENT: -21.25pt; mso-list: l0 level1 lfo1; tab-stops: list 42.25pt"><SPAN
lang=EN-US
style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN
style="mso-list: Ignore">n<SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -