Delphi中如何将参数传递给CreateThread函数实现多线程操作?

2026-04-10 18:532阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计354个文字,预计阅读时间需要2分钟。

Delphi中如何将参数传递给CreateThread函数实现多线程操作?

在调用CreateThread时,将类引用作为参数传递给ThreadProc时遇到问题。以下是一个展示我遇到问题的示例程序:

program test;{ $APPTYPE CONSOLE }uses SysUtils, Windows, Dialogs;type TBlah=class public FInteger: Integer; end;

procedure ThreadProc(ARef: TBlah); begin ShowMessage('Integer Value: ' + IntToStr(ARef.FInteger)); end;

var MyBlah: TBlah; ThreadId: THandle;begin MyBlah :=TBlah.Create; try MyBlah.FInteger :=42; ThreadId :=CreateThread(nil, 0, @ThreadProc, Pointer(MyBlah), 0, nil); if ThreadId=0 then ShowMessage('Failed to create thread.'); finally MyBlah.Free; end;end.

我在调用CreateThread时将类引用作为参数传递给ThreadProc时遇到问题.这是一个演示我遇到的问题的示例程序:

program test; {$APPTYPE CONSOLE} uses SysUtils, Windows, Dialogs; type TBlah = class public fe: Integer; end; function ThreadProc(param: Pointer) : DWORD; begin ShowMessage(IntToStr(TBlah(param).fe)); Result := 0; end; var tID: DWORD; handle: THandle; b: TBlah; begin b := TBlah.Create; b.fe := 54; handle := CreateThread(nil, 0, @ThreadProc, Pointer(b), 0, tID); WaitForSingleObject(handle, INFINITE); end.

对ShowMessage的调用会弹出一个消息框,里面有245729105,而不像我期望的那样.

这可能只是对Delphi如何工作的基本误解,所以有人可以告诉我如何使其正常工作吗?

这里的问题是你的线程函数有错误的调用约定.您需要使用stdcall约定声明它:

function ThreadProc(param: Pointer) : DWORD; stdcall;

话虽如此,仅仅使用一个TThread后代处理OOP到C函数回到OOP过渡会更加惯用.这看起来像这样:

type TBlah = class(TThread) protected procedure Execute; override; public fe: Integer; end; procedure TBlah.Execute; begin ShowMessage(IntToStr(fe)); end; var b: TBlah; begin b := TBlah.Create(True); b.fe := 42; b.Start; b.WaitFor; end.

顺便说一下,有谁知道为什么Windows.pas将TFNThreadStartRoutine声明为TFarProc而不是正确的类型函数指针?

Delphi中如何将参数传递给CreateThread函数实现多线程操作?

本文共计354个文字,预计阅读时间需要2分钟。

Delphi中如何将参数传递给CreateThread函数实现多线程操作?

在调用CreateThread时,将类引用作为参数传递给ThreadProc时遇到问题。以下是一个展示我遇到问题的示例程序:

program test;{ $APPTYPE CONSOLE }uses SysUtils, Windows, Dialogs;type TBlah=class public FInteger: Integer; end;

procedure ThreadProc(ARef: TBlah); begin ShowMessage('Integer Value: ' + IntToStr(ARef.FInteger)); end;

var MyBlah: TBlah; ThreadId: THandle;begin MyBlah :=TBlah.Create; try MyBlah.FInteger :=42; ThreadId :=CreateThread(nil, 0, @ThreadProc, Pointer(MyBlah), 0, nil); if ThreadId=0 then ShowMessage('Failed to create thread.'); finally MyBlah.Free; end;end.

我在调用CreateThread时将类引用作为参数传递给ThreadProc时遇到问题.这是一个演示我遇到的问题的示例程序:

program test; {$APPTYPE CONSOLE} uses SysUtils, Windows, Dialogs; type TBlah = class public fe: Integer; end; function ThreadProc(param: Pointer) : DWORD; begin ShowMessage(IntToStr(TBlah(param).fe)); Result := 0; end; var tID: DWORD; handle: THandle; b: TBlah; begin b := TBlah.Create; b.fe := 54; handle := CreateThread(nil, 0, @ThreadProc, Pointer(b), 0, tID); WaitForSingleObject(handle, INFINITE); end.

对ShowMessage的调用会弹出一个消息框,里面有245729105,而不像我期望的那样.

这可能只是对Delphi如何工作的基本误解,所以有人可以告诉我如何使其正常工作吗?

这里的问题是你的线程函数有错误的调用约定.您需要使用stdcall约定声明它:

function ThreadProc(param: Pointer) : DWORD; stdcall;

话虽如此,仅仅使用一个TThread后代处理OOP到C函数回到OOP过渡会更加惯用.这看起来像这样:

type TBlah = class(TThread) protected procedure Execute; override; public fe: Integer; end; procedure TBlah.Execute; begin ShowMessage(IntToStr(fe)); end; var b: TBlah; begin b := TBlah.Create(True); b.fe := 42; b.Start; b.WaitFor; end.

顺便说一下,有谁知道为什么Windows.pas将TFNThreadStartRoutine声明为TFarProc而不是正确的类型函数指针?

Delphi中如何将参数传递给CreateThread函数实现多线程操作?