详细教你如何使用delphi中thread的线程编程

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 huwenyi 的 blog

       为什么要用线程,简单一点来说吧,省得有些象我一样的新手听不懂,当你编完一个程序,例如是读取文件的程序,如果你读取的文件很大,你的程序如果不是通过线程来执行的话,你一动这个正在读取文件的程序窗口,结果是什么,“死屏”就是你的程序根本拖不动,无法继续做其它的事了,呵呵,如果你用线程,什么问题都解决了,废话少讲,要学的看以下文章吧。

       首先当然是打开你的delphi 6 ,点取菜单栏中的文件-新建-其它,弹出一个标签窗口,选取new标签,然后找到thread object,就是它了,双击它就行了,弹出一个类命名窗口,输入mythread,当然名称可由你自已来定的。这时程序自动创建一个unit,我这里是unit2,现在我们来看unit,代码如下:

unit unit2;

interface

uses
  classes;

type
  mythread = class(tthread)
  private
    { private declarations }
  protected
    procedure execute; override;
  end;

implementation

{ important: methods and properties of objects in vcl or clx can only be used
  in a method called using synchronize, for example,

      synchronize(updatecaption);

  and updatecaption could look like,

    procedure mythread.updatecaption;
    begin
      form1.caption := 'updated in a thread';
    end; }

{ mythread }

procedure mythread.execute;
begin
  { place thread code here }
end;

end.

其中,你注意找到procedure mythread.execute;,应找到了吧,连我都看到了,这就是你刚才建立的线程了,那么接下来,我们要做的就是加入后台执行的代码,代码要加在那里?不会吧,当然是加在
begin
 //这里就是加入程序代码的地方了
end;
如果你要调用unit1上的控件,你可以在unit2上面的uses中加入unit1就行了,记住,在unit1里的implementation后面增加uses unit2,这样你就可在unit1中引用线程了,引用的方法很简单,就是,就是,就是,好啦,不卖关了,就是mythread.create(false);。ok 这就是delphi中的线程,呵呵。

本人刚学delphi 有什么地方说得不对的,欢迎评批指证,我的联系信箱netboy@cnpick.com谢谢!

本文关键:线程、thread
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top