zoukankan      html  css  js  c++  java
  • c++ builder 版CreateAnonymousThread用法

    万一老师的《如今, Delphi 的多线程已经很易用了!》讲到了TThread.CreateAnonymousThread用法

    如今我来讲在c++ builder使用 CreateAnonymousThread。


      要说明的是,c++ builder不能直接照搬Delphi的代码。由于CreateAnonymousThread使用的是Dephi 匿名函数/过程指针。


     1、方法一

      官方论坛谈到用method_cast,但我看过method_cast实现(https://forums.embarcadero.com/thread.jspa?threadID=72457),用到Boost这些东东,姑且不说能不能用,就里面的代码可不适合我等菜鸟。

    TThread::CreateAnonymousThread (
    		method_cast<TProc> (
    			std::tr1::bind (ThreadSafeShowMessage,
                                      message, title, icon)

    2、方法二

      我如今使用这种方法

      

    class TSleepFunc : public TCppInterfacedObject<TProc> {
    public:
    	//这里建议用自己继承的TForm。由于后面所需參数要从Form获得
    	TSleepFunc(TForm3* Form) : FForm(Form) {
    	}
    
    	virtual void __fastcall Invoke(void) {
    	//这里处理你多线程代码
    	//參数所有是 TForm3的变量,多线程中所需參数从TForm中声明
    		CreateXP3Archive(FForm->filename, FForm->LabelPath->Text, 1024,
    			FForm->list, true, true, true, false);
    	//这里处理和TForm交互内容
    		TThread::Synchronize(TThread::CurrentThread, UpdateCaption);
    	}
    
    	void __fastcall UpdateCaption(void) {
    		FForm->Caption = "OK";
    	}
    
    private:
    	TForm3* FForm;
    };
    
    void __fastcall TForm3::ButtonClick(TObject *Sender) {
    TThread::CreateAnonymousThread(new TSleepFunc(this))->Start();
    }
      这里面我演示了多线程调用自己写的函数CreateXP3Archive。以及怎么把參数传入多线程函数中,当中TForm3是我自己firemonkey 窗口。


      另外CreateAnonymousThread是XE版本号以后才提供的函数,相似BCB6这些是没有这个函数的。

  • 相关阅读:
    JS事件学习笔记(思维导图)
    [logstash-input-file]插件使用详解
    echarts折线图,纵坐标数值显示不准确的问题解决
    IDEA 创建maven jar、war、 pom项目
    Lombok介绍、使用方法和总结
    Springboot2.0访问Redis集群
    springboot2.x 整合redis集群的几种方式
    SpringBoot 2.x 使用Redis作为项目数据缓存
    Springboot2.x使用redis作为缓存
    SpringBoot中application.yml基本配置详情
  • 原文地址:https://www.cnblogs.com/llguanli/p/8587298.html
Copyright © 2011-2022 走看看