• 关于Delphi中多线程传递参数的简单问题


    http://bbs.csdn.net/topics/390513469/

    unit uThread;
    
    interface
    
    uses
      Classes;
    
    type
      Th = class(TThread)
      private
        { Private declarations }
       
      protected
        procedure Execute; override;
      end;

    以上是创建的一个多线程
    我在另外一个单元里Unit1有一个函数

    function Myfun(username,password:string):boolean

    现在要把Myfun放到多线程里执行,怎么传递参数呢?
    谁有这样的Demo给一个我,帮我讲解一下,感激不尽!我看到网上说有结构体,因本人才学Delphi没多久,不太懂的,希望大牛们指点迷津!

    解决方法如下:

    // 线程类
    unit Unit2;
    
    interface
    
    uses
      Classes;
    
    type
      TMyThread = class(TThread)
      private
        FUserName: string;
        FPassWord: string;
        FFlag: Boolean;
        procedure GetUserName(const Value: string);
        procedure GetPassWord(const Value: string);
        { Private declarations }
      protected
        procedure Execute; override;
      public
        property UserName: string read FUserName write GetUserName;
        property PassWord: string read FPassWord write GetPassWord;
        property MyFunRetVal: Boolean read FFlag default False;
        function Myfun(FUserName, FPassWord: string): Boolean;
        constructor Create(b: Boolean = True);
      end;
    
    implementation
    
    function TMyThread.Myfun(FUserName, FPassWord: string): Boolean;
    begin
      Result := True; // 简单起见让它固定为True
    end;
    
    constructor TMyThread.Create(b: Boolean = True);
    begin
      inherited Create(b);
      Self.FreeOnTerminate := True;
    end;
    
    procedure TMyThread.Execute;
    begin
      FFlag := Myfun(FUserName, FPassWord);
    end;
    
    procedure TMyThread.GetPassWord(const Value: string);
    begin
      FPassWord := Value;
    end;
    
    procedure TMyThread.GetUserName(const Value: string);
    begin
      FUserName := Value;
    end;
    
    end.
    // 调用单元
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        btn1: TButton;
        procedure btn1Click(Sender: TObject);
      private
      public
        procedure MyOnTerminate(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    uses Unit2; // 线程类定义在unit2单元
    
    var
      retVal: Boolean; // 线程"返回值"
    
    procedure TForm1.btn1Click(Sender: TObject);
    var
      myThread: TMyThread;
    begin
      myThread := TMyThread.Create; // 创建线程并挂起
      myThread.UserName := 'sololie'; // 传参
      myThread.PassWord := '撸过更健康';
      myThread.OnTerminate := MyOnTerminate; // 线程结束的回调函数
      myThread.Resume; // 开始执行线程
    end;
    
    procedure TForm1.MyOnTerminate(Sender: TObject);
    begin
      retVal := (Sender as TMyThread).MyFunRetVal;
      if retVal then
        ShowMessage('线程执行函数返回 True')
      else
        ShowMessage('线程执行函数返回 False');
    end;
    
    end.

    function Myfun(FUserName, FPassWord: string): Boolean; 这行上面加个 private:
    这个函数没必要 也不应该public。

    另外注意,如果线程中涉及操作UI,就把操作UI的代码放到Synchronize中执行

    例如:

    unit Unit2;
    
    interface
    
    uses
      Classes;
    
    type
      TMyThread = class(TThread)
      private
        FUserName: string;
        FPassWord: string;
        FFlag: Boolean;
        procedure GetUserName(const Value: string);
        procedure GetPassWord(const Value: string);
    
        procedure Myfun; // 原本的参数传递交由 TMyThread的实例赋值完成
      protected
        procedure Execute; override;
      public
        property UserName: string read FUserName write GetUserName;
        property PassWord: string read FPassWord write GetPassWord;
        property MyFunRetVal: Boolean read FFlag default False;
        constructor Create(b: Boolean = True);
      end;
    
    implementation
    
    procedure TMyThread.Myfun;
    begin
      // 假设这里做了操作UI的工作
      FFlag := True;
    end;
    
    constructor TMyThread.Create(b: Boolean = True);
    begin
      inherited Create(b);
      Self.FreeOnTerminate := True;
    end;
    
    procedure TMyThread.Execute;
    begin
      // 让操作UI的代码放回主线程中执行已保证同步
      Synchronize(Myfun);
    end;
    
    procedure TMyThread.GetPassWord(const Value: string);
    begin
      FPassWord := Value;
    end;
    
    procedure TMyThread.GetUserName(const Value: string);
    begin
      FUserName := Value;
    end;
    
    end.

    传递函数指针,以回调的形式调用

    type
      TMyFunc = function(username,password:string):boolean;stdcall; 
      Th = class(TThread)  
      private    { Private declarations } 
       FMyFunc : pointer;     
      protected    
        procedure Execute; override; 
      public
        Procedure SetMyFunc(func : pointer); 
      end;
    implementation
    procedure Th.Execute;
    begin
      if assigned(FMyFunc) then TMyFunc(FMyFunc)(username,password);
    end;
    
    procedure Th.SetMyFunc(func : pointer); 
    begin
      FMyFunc := func;
    end;
    
    //在其它地方定义一个函数,比如
    function MyFunc(username,password) :boolean;stdcall;
    begin
      //
    end;
    
    procedure TForm1.btn1Click(Sender: TObject);
     var  myThread: TMyThread;
    begin  
      myThread := TMyThread.Create(false);
      myThread.SetMyFunc(@MyFunc); 
    end;

        property UserName: string read FUserName write GetUserName;
        property PassWord: string read FPassWord write GetPassWord;

    名字都给弄错了,SetUserName、SetPassWord 
        property UserName: string read FUserName write SetUserName;
        property PassWord: string read FPassWord write SetPassWord;

  • 相关阅读:
    唤起支付宝的链接地址
    nginx 403 问题解决
    Mac终端生成RAS秘钥对
    nashPay项目遇到的问题
    redis.clients.jedis.exceptions.JedisDataException 解决方案
    Springboot集成Quartz实现分布式任务调度
    Archives版本mysql5.7.23数据库的安装
    Scala语言操作记录
    搭建wordpress个人博客之(2)安装wordpress
    搭建wordpress个人博客之(1)一键安装lnmp[lamp, lnamp]环境
  • 原文地址:https://www.cnblogs.com/China3S/p/5428922.html
Copyright © 2020-2023  润新知