码迷,mamicode.com
首页 > 编程语言 > 详细

多线程编程(3)--从CreateThread说起(倒数第二个参数)

时间:2015-05-08 14:28:57      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

转载自:万一的博客

 

function CreateThread(
  lpThreadAttributes: Pointer;
  dwStackSize: DWORD;
  lpStartAddress: TFNThreadStartRoutine;
  lpParameter: Pointer;
  dwCreationFlags: DWORD; {启动选项}
  var lpThreadId: DWORD
): THandle; stdcall;

  


  CreateThread的倒数第二个参数dwCreateFlasg(启动选项)有两个可选值

0:线程建立后立即执行入口函数

CREATE_SUSPENDED:线程建立后会挂起等待

  可以ResumeThread 函数恢复线程的运行;可用 SuspendThread再次挂起线程。这两个函数的参数都是线程句柄,返回值都是执行前的挂起计数

  什么是挂起计数?

  SuspendThread会个这个数+1, ResumeThread会给这个数-1;但是这个数最小是0。如果这个数是0,线程会运行; >0 时会挂起。所以如果被SuspendThread 多次,同样需要ResumeThread 多次才能恢复线程的运行

  


  下面的例子中,有新线程不断给一个全局变量赋随机值

  同时窗体上的Timer 控件每隔 1/10秒就把这个变量写在窗体标题

  在这个过程中演示了ResumeThread 和SuspendThread这两个函数,运行效果图如下

技术分享

  代码如下

unit Unit1;

interface
uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
    TForm1=class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Timer1: TTimer;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
    end;

var
    Form1: TForm1;

implementation
{$R *.dfm}

var
    hThread: THandle;    //线程句柄
    num: Integer;        //全局变量,用于记录随机数

{线程入口函数}
function MyThreadFun(p: Pointer): Integer; stdcall;
begin
    while True do
    begin
        num:= Random(100);
    end;
    Result:= 0;
end;

{建立并挂起线程}
procedure TForm1.Button1Click(Sender: TObject);
var
    ID: DWORD;
begin
    hThread:= CreateThread(nil, 0, @MyThreadFun, nil, CREATE_SUSPENDED, ID);
    Button1.Enabled:= False;
end;

{唤醒并继续线程}
procedure TForm1.Button2Click(Sender: TObject);
begin
    ResumeThread(hThread);
end;

{挂起线程}
procedure TForm1.Button3Click(Sender: TObject);
begin
    SuspendThread(hThread);
end;

{下面是窗体代码}
procedure TForm1.FormCreate(Sender: TObject);
begin
    Timer1.Interval:= 100;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
    Text:= IntToStr(num);
end;

end.

  

  下面是窗体文件

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = ‘Form1‘
  ClientHeight = 86
  ClientWidth = 269
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = ‘Tahoma‘
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 16
    Top = 24
    Width = 75
    Height = 25
    Caption = ‘Button1‘
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 97
    Top = 24
    Width = 75
    Height = 25
    Caption = ‘Button2‘
    TabOrder = 1
    OnClick = Button2Click
  end
  object Button3: TButton
    Left = 178
    Top = 24
    Width = 75
    Height = 25
    Caption = ‘Button3‘
    TabOrder = 2
    OnClick = Button3Click
  end
  object Timer1: TTimer
    OnTimer = Timer1Timer
  end
end

  


  ResumeThread 和SuspendThread 分别对应 TThread的Resume 和 Suspend 方法,很好理解

  接下来应该是 CreateThread的第四个参数了(也就是倒数第三个参数)

 

多线程编程(3)--从CreateThread说起(倒数第二个参数)

标签:

原文地址:http://www.cnblogs.com/xumenger/p/4487477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!