zoukankan      html  css  js  c++  java
  • Delphi另一个多线程函数:BeginThread用法

    Delphi另一个多线程函数:BeginThread
    ━━━━━━━━━━━━━━━━━━━━━━━━━━

    Delphi也提供了一个相同功能的类似函数:
    function BeginThread(
        SecurityAttributes: Pointer; 
        StackSize: LongWord; 
        ThreadFunc: TThreadFunc; 
        Parameter: Pointer; 
        CreationFlags: LongWord; 
        var ThreadId: LongWord
    ): Integer;
    ━━━━━━━━━━━━━━━━━━━━━━━━━━
    FHandle := BeginThread(nil, 0, @ThreadProc, Pointer(Self), CREATE_SUSPENDED, FThreadID);
    这里就用到了前面说到的Delphi RTL函数BeginThread,它有很多参数,关键的是第三、四两个参数。
    第三个参数就是前面说到的线程函数,即在线程中执行的代码部分。
    第四个参数则是传递给线程函数的参数,在这里就是创建的线程对象(即Self)。
    第五个是用于设置线程在创建后即挂起,不立即执行(启动线程的工作是在AfterConstruction中根据CreateSuspended标志来决定的),
    第六个是返回线程ID。

    ━━━━━━━━━━━━━━━━━━━━━━━━━━
    定义被BeginThread调用的函数
    ━━━━━━━━━━━━━━━━━━━━━━━━━━

    声明:type TThreadFunc : Function(Parameter : Pointer) : Integer;

    描述:TThreadFunc定义了一个函数而不是数据。这个函数通常作为BeginThread函数的一个参数,BeginThread是一个单独的线程运行时开始的。这个定义的TThreadFunc函数实现了线程的行为。

    函数的返回代码是线程的退出代码。

    uses
    Forms, Dialogs, Windows, SysUtils;

    type
    TMsgRecord = record
        thread : Integer;
        msg    : string[30];
    end;
    TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
    end;

    var
    Form1: TForm1;

    Implementation
    {$R *.dfm}        // 包含窗体定义

    ThreadVar         // 必须允许每个线程有被传递增的记录变量的实例

    msgPtr : ^TMsgRecord;

    // 私有的线程过程,用于显示字符串
    function ShowMsg(Parameter : Pointer) : Integer;
    begin
    // 设返回值为0
    Result := 0;

    // 映射指针到被传递进来的参数
    // 每个线程有一份独立的msgPtr副本
    msgPtr := Parameter;

    // 在指定坐标显示对话框
    ShowMessagePos('Thread '+IntToStr(msgPtr.thread)+' '+msgPtr.msg,
                     200*msgPtr.thread, 100);

    // 结束线程
    EndThread(0);
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    var
    id1, id2 : LongWord;
    thread1, thread2 : Integer;
    msg1, msg2 : TMsgRecord;
    showMsgFunc : TThreadFunc;

    begin
    // 指定线程函数地址
    showMsgFunc := Addr(ShowMsg);

    //初始化要显示的
    msg1.thread := 1;
    msg1.msg    := 'Hello World';
    msg2.thread := 2;
    msg2.msg    := 'Goodbye World';

    // 开始第一个线程
    thread1 := BeginThread(nil,
                             0,
                             showMsgFunc,
                             Addr(msg1),
                             0,
                             id1);

    // 开始第二个线程
    thread2 := BeginThread(nil,
                             0,
                             showMsgFunc,
                             Addr(msg2),
                             0,
                             id2);

    // 确保线程关闭前,线程应执行完成
    ShowMessagePos('Press this when other dialogs finished.', 200, 300);

    // 最后关闭线程
    CloseHandle(thread1);
    CloseHandle(thread2);
    end;
    end.

    程序运行结果,显示3个对话框:
    Thread 1 Hello World
    Thread 2 Goodbye World
    Press this when other dialogs finished.

    转自https://www.xuebuyuan.com/467050.html

  • 相关阅读:
    如何防止多个人同时编辑文件
    通过Word实现表单套打
    偏前端
    偏前端
    偏前端
    偏前端 -webpack打包之(安装webpack)
    偏前端
    偏前端
    偏前端
    偏前端--之小白学习本地存储与cookie
  • 原文地址:https://www.cnblogs.com/zyb2016/p/11151918.html
Copyright © 2011-2022 走看看