zoukankan      html  css  js  c++  java
  • Delphi主线程等待子线程

    -----------Delphi7

    这个例子是别人的,忘记出处了,坑

    ---------------------

    -------------------Unit

    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, DB, ADODB;

    type
    TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo2: TMemo;
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;
    TMythread=class(TThread)
    protected
    procedure Execute;override;

    end;

    TMyADOSQL=class(TObject)
    private
    FMyADOconnect:TADOConnection;
    FMyADOquery:TADOQuery;
    FDelyTime:Word;//秒
    FBool:Boolean;//SQL执行完标志
    FBeginFlag:Boolean;//
    public
    constructor Create(ADODelyTime:Word);
    procedure PExecSQL;
    procedure PRODelyTime;
    end;
    var
    Form1: TForm1;
    Critical:TRTLCriticalSection ;
    A:Integer;
    MyThread:TMythread;
    implementation

    {$R *.dfm}
    var
    WaitLockBool: boolean; //主线程的该操作是否处于等待状态
    function PubF(Ini:Integer ;var AA:integer):string;
    var
    vstr:string;
    i:Integer;
    begin

    EnterCriticalSection(Critical);//进入临界段
    Form1.Memo2.Lines.Add('开启');
    for i:=0 to 2 do
    begin
    Sleep(1000);
    Application.ProcessMessages ;
    inc(AA);

    Form1.Memo2.Lines.Add('Str_'+inttostr(AA)+' '+inttostr(i)+' '+ IntToStr(ini));
    end;
    Form1.Memo2.Lines.Add('关闭');
    Result :='Str_'+inttostr(AA)+' '+inttostr(i)+' '+ IntToStr(ini);
    LeaveCriticalSection(Critical);//退出临界段
    end;
    procedure PMainWaitLock; //主线程等待临界区执行完成
    var
    i,vCount: integer;
    begin
    WaitLockBool := true;
    vCount:=1200; //如果看不出效果,请把这个参数改小,改成vCount:=1200 或者120
    for i:=1 to vCount do //超时时长,可根据实际情况进行设置
    begin
    if Critical.LockCount<>-1 then
    begin
    Application.ProcessMessages;
    sleep(1); //休眠1毫秒
    end
    else
    break;
    end;
    if (i<vCount) then
    WaitLockBool := false;
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    InitializeCriticalSection(Critical);
    end;

    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
    DeleteCriticalSection(Critical);
    if MyThread<>nil then
    if not MyThread.Terminated then
    begin
    MyThread.Terminate;
    end;
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    MyThread:=TMythread.Create(False);
    end;

    { TMythread }

    procedure TMythread.Execute;
    var
    i,AA:Integer;

    begin
    AA:=0;
    for I := 1 to 2 do
    begin
    Sleep(1000);
    PubF(i,AA);
    end;

    end;

    procedure TForm1.Button2Click(Sender: TObject);
    var
    AA:Integer;
    begin
    AA:=1000;
    PMainWaitLock;
    if not WaitLockBool then
    PubF(10,AA)
    else
    ShowMessage('被占用,请重试!')
    end;

    procedure TForm1.Button3Click(Sender: TObject);
    var
    TMyADOSQL_01:TMyADOSQL;
    begin
    TMyADOSQL_01:=TMyADOSQL.Create(2);
    end;

    //-------------------------------------------- 以下没有什么意义,不放在多线程中,会卡住界面
    { TMyADOSQL }

    constructor TMyADOSQL.Create(ADODelyTime: Word);
    begin
    inherited Create;
    FMyADOconnect:=TADOConnection.Create(nil);

    FMyADOconnect.CommandTimeout :=Form1.ADOConnection1.CommandTimeout;
    FMyADOconnect.ConnectionTimeout :=Form1.ADOConnection1.ConnectionTimeout;
    FMyADOconnect.ConnectionString :=Form1.ADOConnection1.ConnectionString;
    FMyADOconnect.LoginPrompt :=Form1.ADOConnection1.LoginPrompt;
    FMyADOconnect.Provider :=Form1.ADOConnection1.Provider;
    FMyADOconnect.DefaultDatabase :=Form1.ADOConnection1.DefaultDatabase;
    FMyADOconnect.Connected:=True;
    FMyADOquery:=TADOQuery.Create(nil);
    FMyADOquery.Connection:=FMyADOconnect;
    FMyADOquery.CommandTimeout:=10;
    FDelyTime:= ADODelyTime;
    //FMyADOconnect.CommandTimeout=1000*FDelyTime;
    PExecSQL;
    PRODelyTime;
    end;

    procedure TMyADOSQL.PExecSQL;
    begin
    try
    Fbool:=False;
    FBeginFlag:=True;
    //延时这么久是为了测试
    if not FMyADOconnect.Connected then
    FMyADOconnect.Connected:=True;
    FMyADOconnect.BeginTrans;
    Sleep(1000);
    FMyADOquery.SQL.Text:='select * from dbo.Exists_Test';
    FMyADOquery.Close;
    FMyADOquery.Open;
    Sleep(1000);
    Sleep(1000);
    FMyADOquery.SQL.Text:=Format(' begin Update dbo.Exists_Test set MydateTime=''%s'' where Myid=1 waitfor delay ''00:00:20'' end;',[DatetimeToStr(now)]);
    FMyADOquery.Prepared:=True;
    FMyADOquery.ExecSQL;
    Sleep(1000);
    except

    FMyADOconnect.RollbackTrans;
    Fbool:=True;//
    Exit;
    end;
    FMyADOconnect.CommitTrans;
    Fbool:=True;
    end;

    procedure TMyADOSQL.PRODelyTime;
    var
    i:Integer;
    begin
    i:=0;
    while True do
    begin
    Application.ProcessMessages;
    if i>FDelyTime*100 then
    begin
    if Fbool then //已经执行完毕
    begin
    Break;
    end
    else
    begin
    if FMyADOconnect<>nil then
    begin
    FMyADOconnect.RollbackTrans;
    Break;
    end;
    end;
    end;
    if FBeginFlag then
    inc(i,10);
    Sleep(10);
    end;
    end;

    end.

    --------------------------------------------------------unit结束

    ----------Form

    object Form1: TForm1
    Left = 801
    Top = 209
    Width = 326
    Height = 479
    Caption = 'Form1'
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    OldCreateOrder = False
    OnClose = FormClose
    OnCreate = FormCreate
    PixelsPerInch = 96
    TextHeight = 13
    object Button1: TButton
    Left = 48
    Top = 384
    Width = 75
    Height = 25
    Caption = 'B1_线程'
    TabOrder = 0
    OnClick = Button1Click
    end
    object Button2: TButton
    Left = 192
    Top = 384
    Width = 75
    Height = 25
    Caption = 'B2主'
    TabOrder = 1
    OnClick = Button2Click
    end
    object Memo2: TMemo
    Left = 24
    Top = 16
    Width = 273
    Height = 241
    ImeName = '中文(简体) - 搜狗拼音输入法'
    ScrollBars = ssBoth
    TabOrder = 2
    end
    object Button3: TButton
    Left = 96
    Top = 672
    Width = 75
    Height = 25
    Caption = 'Button3'
    TabOrder = 3
    OnClick = Button3Click
    end
    object ADOConnection1: TADOConnection
    ConnectionString =
    'Provider=SQLOLEDB.1;Password=sa123456;Persist Security Info=True' +
    ';User ID=sa;Initial Catalog=HJP;Data Source=.SQLexpress'
    ConnectionTimeout = 30
    LoginPrompt = False
    Provider = 'SQLOLEDB.1'
    Left = 8
    Top = 672
    end
    object ADOQuery1: TADOQuery
    CommandTimeout = 10
    Parameters = <>
    Left = 48
    Top = 672
    end
    end

    --------------------------Form结束

  • 相关阅读:
    python3使用多代理访问网站
    批处理写的俄罗斯方块
    Robot Framework安装指南
    Robot Framework 快速入门_英文版
    Robot Framework 快速入门_中文版
    python3操作注册表设置/取消IE代理
    在python3下用PIL做图像处理
    python win32api 使用小技巧
    Python小技巧1
    python编码-1
  • 原文地址:https://www.cnblogs.com/dmqhjp/p/14626246.html
Copyright © 2011-2022 走看看