delphi 字符串函数_C语言字符串函数

delphi 字符串函数_C语言字符串函数QueueUserWorkItem函数Windows说明如下:一、异步调用函数:BOOLQueueUserWorkItem(PTHREAD_START_ROUTINEpfnCallback,PVOIDpvContext,ULONGdwFlags);该函数将“工作项目”放入线程池并且立即返回。工作项目是指一个用pfnCallback参数标识的函数。它被调用并且传递…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

 QueueUserWorkItem 函数 Windows 说明如下:

 

一、异步调用函数:
BOOL QueueUserWorkItem(
PTHREAD_START_ROUTINE pfnCallback,
PVOID pvContext,
ULONG dwFlags);
该函数将“工作项目”放入线程池并且立即返回。工作项目是指一个用pfnCallback参数标识的函数。它被调用并且传递单个参数pvContext.工作项目函数原型如下:
DWORD WINAPI WorkItemFunc(PVOID pvContext);
dwFlags参数:WT_EXECUTEDEFAULT  工作项目放入非I/O组件得线程中
             WT_EXECUTEINIOTHREAD 工作项目放入I/O组件的线程中,这样的线程在I/O请求没有完成之前不会被终止运行                                  ,防止因为线程被终止导致I/O请求丢失。
             WT_EXECUTEINPERSISTENTTHREAD 放入永久线程池,
             WT_EXECUTELONGFUNCTION  工作项目需要长时间的工作,系统会据此安排更多的线程。

线程池不能设置线程个数的上限,否则排队个数超过线程个数上限的时候,会导致所有的线程都被中断。

工作项目函数如果访问了已经被卸载的DLL,会产生违规访问。

 

delphi 官网给出的调用例子:

unit ThreadPoolUnit;

interface

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

type
  TForm3
= class(TForm)
    PaintBox1: TPaintBox;
    Button1: TButton;
    ListBox1: TListBox;
   
procedure Button1Click(Sender: TObject);
  private
   
type
      TWorkerColor
= class
        FThreadID: Integer;
        FColor: TColor;
        FForm: TForm3;
       
procedure PaintLines(Sender: TObject);
       
procedure PaintLine;
        constructor Create(AForm: TForm3; AColor: TColor);
     
end;
   
var
      FIndex: Integer;
  public
   
{
Public declarations }
 
end;

  TObjectHelper = class helper for TObject

  end;

  TThreadPool = class
  private
   
type
      TUserWorkItem
= class
        FSender: TObject;
        FWorkerEvent: TNotifyEvent;
     
end;
    class
procedure QueueWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent; Flags: ULONG); overload; static;
  public
    class
procedure QueueWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent); overload; static;
    class
procedure QueueIOWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent); static;
    class
procedure QueueUIWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent); static;
 
end;

var
  Form3: TForm3;
  ThreadPool: TThreadPool;

implementation

{
$R *.dfm}

const
  WT_EXECUTEDEFAULT      
= ULONG($00000000);
  WT_EXECUTEINIOTHREAD   
= ULONG($00000001);
  WT_EXECUTEINUITHREAD   
= ULONG($00000002);
  WT_EXECUTEINWAITTHREAD 
= ULONG($00000004);
  WT_EXECUTEONLYONCE     
= ULONG($00000008);
  WT_EXECUTEINTIMERTHREAD
= ULONG($00000020);
  WT_EXECUTELONGFUNCTION 
= ULONG($00000010);
  WT_EXECUTEINPERSISTENTIOTHREAD 
= ULONG($00000040);
  WT_EXECUTEINPERSISTENTTHREAD
= ULONG($00000080);
  WT_TRANSFER_IMPERSONATION
= ULONG($00000100);

function QueueUserWorkItem (func: TThreadStartRoutine; Context: Pointer; Flags: ULONG): BOOL; stdcall; external kernel32 name QueueUserWorkItem;

function InternalThreadFunction(lpThreadParameter: Pointer): Integer; stdcall;
begin
  Result :
= 0;
  try
    try
     
with TThreadPool.TUserWorkItem(lpThreadParameter) do
       
if Assigned(FWorkerEvent) then
          FWorkerEvent(FSender);
    finally
      TThreadPool.TUserWorkItem(lpThreadParameter).Free;
   
end;
  except

  end;
end;

{
TThreadPool }

class procedure TThreadPool.QueueWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent);
begin
  QueueWorkItem(Sender, WorkerEvent, WT_EXECUTEDEFAULT);
end;

class procedure TThreadPool.QueueIOWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent);
begin
  QueueWorkItem(Sender, WorkerEvent, WT_EXECUTEINIOTHREAD);
end;

class procedure TThreadPool.QueueUIWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent);
begin
  QueueWorkItem(Sender, WorkerEvent, WT_EXECUTEINUITHREAD);
end;

class procedure TThreadPool.QueueWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent; Flags: ULONG);
var
  WorkItem: TUserWorkItem;
begin
 
if Assigned(WorkerEvent) then
 
begin
    IsMultiThread :
= True;
    WorkItem :
= TUserWorkItem.Create;
    try
      WorkItem.FWorkerEvent :
= WorkerEvent;
      WorkItem.FSender :
= Sender;
     
if not QueueUserWorkItem(InternalThreadFunction, WorkItem, Flags) then
        RaiseLastOSError;
    except
      WorkItem.Free;
      raise;
   
end;
end;
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  FIndex :
= PaintBox1.Height;
  PaintBox1.Repaint;
  ListBox1.Items.Clear;
  TWorkerColor.Create(Self, clBlue);
  TWorkerColor.Create(Self, clRed);
  TWorkerColor.Create(Self, clYellow);
  TWorkerColor.Create(Self, clLime);
  TWorkerColor.Create(Self, clFuchsia);
  TWorkerColor.Create(Self, clTeal);
end;

{
TForm3.TWorkerColor }

constructor TForm3.TWorkerColor.Create(AForm: TForm3; AColor: TColor);
begin
  FForm :
= AForm;
  FColor :
= AColor;
  TThreadPool.QueueWorkItem(Self, PaintLines);
end;

procedure TForm3.TWorkerColor.PaintLines(Sender: TObject);
var
  I: Integer;
begin
  FThreadID :
= GetCurrentThreadID;
 
for I := 0 to 9 do
 
begin
    PaintLine;
   
//TThread.Synchronize(nil, PaintLine);
    Sleep(
100);
 
end;
  Destroy;
end;

procedure TForm3.TWorkerColor.PaintLine;
begin
  FForm.PaintBox1.Canvas.Lock;
  try
    FForm.ListBox1.Items.Add(IntToStr(FThreadID));
   
with FForm.PaintBox1 do
   
begin
      Canvas.Pen.Color :
= FColor;
      Canvas.Polyline([Point(
0, FForm.FIndex), Point(Width, FForm.FIndex)]);
      Dec(FForm.FIndex);
     
if FForm.FIndex <= 0 then
        FForm.FIndex :
= 0;
   
end;
  finally
    FForm.PaintBox1.Canvas.Unlock;
 
end;
end;

end.

调用搞得太复杂,得弄一个简单的

转载于:https://www.cnblogs.com/MLKJ/archive/2010/11/30/1892433.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/192937.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 别再浪费顶头上司的时间了::哈佛商业评论::每日管理小贴士

    别再浪费顶头上司的时间了::哈佛商业评论::每日管理小贴士

    2021年8月19日
    58
  • python hexdump_细说Linux中怎么用hexdump命令

    python hexdump_细说Linux中怎么用hexdump命令摘要:hexdump描述:hexdump命令一般用来查看”二进制”文件的十六进制编码,从手册上查看,其查看的内容还要很多,诸如:ascii,decimal,hexadecimal,octal参数:hexdump[-bcCdovx][-eformat_string][-fformat_file][-nlength][-sskip]file示例:新增一个文本文件,在test…

    2022年9月16日
    0
  • python菜鸟踩坑系列-pika版本带来的问题

    python菜鸟踩坑系列-pika版本带来的问题

    2021年5月16日
    205
  • Tfs权限设置_设置朋友圈权限对方知道吗

    Tfs权限设置_设置朋友圈权限对方知道吗tfs账号分两种情况,一种是基于AD域的 一种是基于Windows账号要使用基于AD域的,tfs必须基于域用户安装。一般会单独建一个tfs的域帐号用来管理tfs用。基于windows的多数都是直接用administrator账号了。tfs增加用户的时候,基于域的直接选择域用户,基于windows账号的直接选择本机的windows账号即可添加用户到tfs后,可

    2022年10月25日
    0
  • VMware P2V报错

    VMware P2V报错VMwareP2Vwin2008R2操作系统,第一步报错:fault.agentinstallfault.summary,无法在源主机临时安装agent代理,手工将vmware-converter-agent安装程序传到源主机安装,提示报错:error29142couldnotstartservicevstor2mntapi2.0driver(shared),原因是这台源主机以前

    2022年7月16日
    51
  • Unity AssetBundle

    Unity AssetBundle#AssetBundle作用原理把资源导出成一种叫做AssetBundle的文件,然后打包后可以在Unity程序运行的时候再加载回来用。AssetBundle是采取某一种压缩方式压缩成的资源文件。节省存储空间,控制游戏包的大小,实现游戏的热更新。AssetBundle文件分类AssetBundle文件可以分为两类:序列化文件(serializedfile)和资源文件(resource…

    2022年6月24日
    22

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号