⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cb199910db_f.asp.htm

📁 C++builder学习资料C++builder
💻 HTM
📖 第 1 页 / 共 3 页
字号:
      -----------------------------------------------------</SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><B>class</B> TShapeThread : <B>public</B>   

      TThread</SPAN></P>  

      <P class=Code><SPAN class=Code>{</SPAN></P>  

      <P class=Code><SPAN class=Code><B>&nbsp;&nbsp;private</B>:</SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp;&nbsp;&nbsp;<I> <SPAN   

      class=CodeBlue>// Private declarations. </SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp;&nbsp;&nbsp;TShape *FShape;   

      </SPAN></P>  

      <P class=Code><SPAN class=Code><B>&nbsp;&nbsp;protected</B>:</SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp;&nbsp;&nbsp;<B> void</B>   

      <B>__fastcall</B> Execute(); <I style="mso-bidi-font-style: normal"><SPAN   

      class=CodeBlue>// Override default function. </SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><B>&nbsp;&nbsp;public</B>:</SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp;&nbsp;&nbsp;<B> virtual</B>   

      <B>__fastcall</B> TShapeThread(TShape *Shape); <I><SPAN class=CodeBlue>//   

      Constructor. </SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp;&nbsp;&nbsp;<B> void</B>   

      <B>__fastcall</B> ChangeShape();&nbsp;&nbsp;<I> <SPAN class=CodeBlue>//   

      Function to change shape. </SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><B>&nbsp;&nbsp;__published</B>:</SPAN></P>  

      <P class=Code><SPAN class=Code>};</SPAN></P>  

      <P class=Code><SPAN class=Code><I><SPAN class=CodeBlue>//   

      -----------------------------------------------------</SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><SPAN   

class=CodeGrn>#endif</SPAN></SPAN></P>  

      <P class=Captions><B>Figure 2:</B> The object class declaration for   

      <I>TShapeThread</I>.</P>  

      <P class=BodyText> </P>  

      <P class=BodyText>The execution code for the <I>TShapeThread</I> object is   

      shown in Figure 3. </P>  

      <P class=BodyText> </P>  

      <P class=Code><SPAN class=Code><I><SPAN class=CodeBlue>//   

      -----------------------------------------------------</SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><SPAN class=CodeGrn>#include   

      &lt;vcl\vcl.h&gt; </SPAN></SPAN></P>  

      <P class=Code><SPAN class=Code><SPAN class=CodeGrn>#pragma   

      hdrstop</SPAN></SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp; </SPAN></P>  

      <P class=Code><SPAN class=Code><SPAN class=CodeGrn>#include "sthread.h"   

      </SPAN></SPAN></P>  

      <P class=Code><SPAN class=Code><I><SPAN class=CodeBlue>//   

      -----------------------------------------------------</SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><B>static</B> <B>inline</B> TShapeThread   

      *ValidCtrCheck()</SPAN></P>  

      <P class=Code><SPAN class=Code>{</SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp;<B> return</B> <B>new</B>   

      TShapeThread(NULL); </SPAN></P>  

      <P class=Code><SPAN class=Code>}</SPAN></P>  

      <P class=Code><SPAN class=Code><I><SPAN class=CodeBlue>//   

      -----------------------------------------------------</SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><B>__fastcall</B>   

      TShapeThread::TShapeThread(TShape *Shape) </SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp;:   

      TThread(<B>true</B>)</SPAN></P>  

      <P class=Code><SPAN class=Code>{</SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;FShape = Shape; </SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;FreeOnTerminate =   

      <B>true</B>;</SPAN></P>  

      <P class=Code><SPAN class=Code>}</SPAN></P>  

      <P class=Code><SPAN class=Code><I><SPAN class=CodeBlue>//   

      -----------------------------------------------------</SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><B>namespace</B> Sthread</SPAN></P>  

      <P class=Code><SPAN class=Code>{</SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp;<B> void</B> <B>__fastcall</B>   

      Register()</SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp; {</SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp;&nbsp;&nbsp;TComponentClass   

      classes[1] = {<B   

      style="mso-bidi-font-weight: normal">__classid</B>(TShapeThread)};   

      </SPAN></P>  

      <P class=Code><SPAN   

      class=Code>&nbsp;&nbsp;&nbsp;&nbsp;RegisterComponents("Samples", classes,   

      0); </SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp;} </SPAN></P>  

      <P class=Code><SPAN class=Code>}</SPAN></P>  

      <P class=Code><SPAN class=Code><I><SPAN class=CodeBlue>//   

      -----------------------------------------------------</SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><I><SPAN class=CodeBlue>// Our new execute   

      method calls the ChangeShape function along</SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><I><SPAN class=CodeBlue>// with the   

      Synchronize method because we are changing an object</SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><I><SPAN class=CodeBlue>// on the form.   

      </SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><B>void</B> <B   

      style="mso-bidi-font-weight: normal">__fastcall</B>   

      TShapeThread::Execute()</SPAN></P>  

      <P class=Code><SPAN class=Code>{</SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;<B> while</B> (!Terminated)   

      </SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;{ </SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp;Synchronize(ChangeShape);   

      </SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;} </SPAN></P>  

      <P class=Code><SPAN class=Code>}</SPAN></P>  

      <P class=Code><SPAN class=Code><I><SPAN class=CodeBlue>//   

      -----------------------------------------------------</SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><I><SPAN class=CodeBlue>// Change shape to   

      next shape in list. </SPAN></I></SPAN></P>  

      <P class=Code><SPAN class=Code><B>void</B> <B   

      style="mso-bidi-font-weight: normal">__fastcall</B>   

      TShapeThread::ChangeShape()</SPAN></P>  

      <P class=Code><SPAN class=Code>{</SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;<B> if</B> (FShape-&gt;Shape ==   

      stCircle) </SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp;FShape-&gt;Shape = stRectangle;   

      </SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;<B> else</B></SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;&nbsp;FShape-&gt;Shape =   

      TShapeType((FShape-&gt;Shape)+1); </SPAN></P>  

      <P class=Code><SPAN class=Code>&nbsp;FShape-&gt;Refresh();</SPAN></P>  

      <P class=Code><SPAN class=Code>}</SPAN></P>  

      <P class=Code><SPAN class=Code><I><SPAN class=CodeBlue>//   

      -----------------------------------------------------</SPAN></I></SPAN></P>  

      <P class=Captions><B>Figure 3:</B> The execution code for   

      <I>TShapeThread</I>.</P>  

      <P class=BodyText> </P>  

      <P class=BodyText>C++Builder makes it easy to create a new <I>TThread</I>   

      class. Just select File | New from the main menu; then select Thread   

      Object from the New page of the New Items dialog box (see Figure 4). This   

      creates the shell from which we can create our new thread class. (Figure 3   

      shows the code after the shell has been filled.) </P>  

      <P class=BodyText> </P>  

      <P class=Captions><IMG align=left height=334   

      src="images/cb199910db_f_image004.jpg" width=333   

      tppabs="http://www.cbuilderzine.com/features/1999/10/cb199910db_f/cb199910db_f_image004.jpg">   

      <BR clear=all style="mso-ignore: vglayout"><B>Figure 4:</B> The New Items   

      dialog box. </P>  

      <P class=BodyText> </P><BR clear=all>  

      <P class=BodyText>In our sample application, each thread is created with   

      the <I>CreateSuspended</I> property set to <B>true</B>, has a custom <I   

      style="mso-bidi-font-style: normal">Execute</I> method to call the   

      <I>Change</I> method for the shape of the thread object, and has the   

      <I>Change</I> method itself. The <I>ChangeShape</I> procedure simply   

      changes the shape of the thread object equal to the next shape in the <I   

      style="mso-bidi-font-style: normal">TShapeType</I> list. This procedure is   

      called using <I>Synchronize</I> so that while the <I>TShape</I> component   

      is being accessed, there won't be any conflict with other threads.   

      Although there are no other threads that will access the <I>TShape</I>   

      object of each individual thread in this example, it's safe programming to   

      use it in this case because we're accessing an object in another situation   

      that may be shared with other threads. </P>  

      <P class=BodyText> </P>  

      <P class=BodyText>Running the multithreaded shape-changing application by   

      itself may not immediately show the effect of running multiple processes   

      at the same time. To really see how different threads can be given   

      different priorities, run a task in the background of Windows while the   

      application is running, such as copying files from one directory to   

      another. The shapes set to the lower priorities will be very slow to   

      change shape, and the higher priority threads will continue to run   

      quickly. It's important to take into consideration which processes, if   

      any, will be run in addition to your application and set up the priorities   

      of your threads accordingly. </P>  

      <P class=BodyText> </P>  

      <P class=Subheads>Conclusion</P>  

      <P class=BodyText>As you can see from this simple example, once the thread   

      object has been set up correctly, it's fairly straightforward to create   

      new thread objects and set priorities. In Part II of this two-part series,   

      we'll move on to a more "real world" example by implementing multithreaded   

      database queries. </P>  

      <P class=BodyText> </P>  

      <P class=BodyText><I>The project referenced in this article is available   

      for <a href="download/cb199910db_f.zip" tppabs="http://www.cbuilderzine.com/features/1999/10/cb199910db_f/cb199910db_d.asp">download</a>.  

      </I></P> 

      <P class=BodyText> </P> 

      <P class=Biotext>Dennis P. Butler is a Senior Consultant for Inprise   

      Corp., based out of the Professional Services Organization office in   

      Marlboro, MA. He has presented numerous talks at Inprise Developers   

      Conferences in both the US and Canada, and has written a variety of   

      articles for various technical magazines, including <I>Delphi Informant   

      Magazine</I>. He can be reached at <A   

      href="mailto:dbutler@inprise.com">mailto:dbutler@inprise.com</A>, or (508)   

      481-1400.</P>  

      <!-- Abstract<p class=Abstract>C++Builder  

gives application developers the ability to easily take advantage of  

multithreading through the framework of the <i>TThread</i>  

class. Mr Butler begins a two-part series demonstrating how programmers can now  

maximize application efficiency by taking advantage of multiple threads in  

their applications rather than having to run one process at a time. </p>Abstract -->  

      <!-- Blurb<p class=Blurb>Introduction to multithreading with C++Builder.</p>Blurb -->  

    </TD></TR></TBODY></TABLE>  

<!-- <PubToken>cb</PubToken> --></BODY></HTML>  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -