gpt4 book ai didi

multithreading - delphi中的DLL、表单和线程(合一)问题

转载 作者:行者123 更新时间:2023-12-03 15:45:34 26 4
gpt4 key购买 nike

我尝试构建一个非常复杂的应用程序。

有一个我创建的 DLL 库。我在其中放置了一个表单,并在其中放置了一个线程。

在 DLL 中我有一个函数:

procedure ShowForm; stdcall;
var
Form1 : TFormSNVFL7;
begin
Form1 := TFormSNVFL7.Create(nil);
Form1.Show;
end;

我创建一个表单并显示它。这里没有问题。我向这个 dll 添加了一个线程。我在表格上放了一个计时器。几秒钟后,我创建一个线程并运行它。一切都很正常,但是当我尝试更改任何形式时,什么也没有发生。

在同步功能中,我尝试更改其上的标签,但没有任何反应。

这是文件:

DLL 路径:

library uploader;

uses
SysUtils,
Classes,
Forms,
UploaderForm in 'UploaderForm.pas' {FormUploader},
ThreadUpload in 'ThreadUpload.pas';

{$R *.res}

procedure ShowForm; stdcall;
var
upForm: TFormUploader;
begin
upForm := TFormUploader.Create(nil);
upForm.Show;
end;

exports
ShowForm;

begin
end.

表格:

unit UploaderForm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, acPNG, ExtCtrls, JvExExtCtrls, JvImage, JvExControls, JvLabel,
JvAnimatedImage, JvGIFCtrl, ComCtrls, JvExComCtrls, JvProgressBar, StdCtrls,
FileCtrl, JvDriveCtrls;

type
TFormUploader = class(TForm)
imgRunning: TJvImage;
imgReady: TJvImage;
imgUpdate: TJvImage;
JvLabel1: TJvLabel;
JvLabel2: TJvLabel;
imgConnect: TJvImage;
imgUpload: TJvImage;
imgCheck: TJvImage;
JvLabel3: TJvLabel;
JvLabel4: TJvLabel;
JvLabel5: TJvLabel;
JvLabel6: TJvLabel;
imgRun: TJvImage;
imgOK: TJvImage;
imgDone: TJvImage;
JvProgressBar1: TJvProgressBar;
JvLabel7: TJvLabel;
fileList: TJvFileListBox;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
FormUploader: TFormUploader;

implementation

{$R *.dfm}

Uses ThreadUpload;

procedure TFormUploader.FormCreate(Sender: TObject);
begin
imgUpdate.Picture := imgReady.Picture;
imgConnect.Picture := imgReady.Picture;
imgUpload.Picture := imgReady.Picture;
imgCheck.Picture := imgReady.Picture;
imgRun.Picture := imgReady.Picture;
imgOK.Picture := imgReady.Picture;
fileList.Directory := ExtractFilePath(Application.ExeName) + 'csvexport/';
end;

procedure TFormUploader.Timer1Timer(Sender: TObject);
var
UpThread: TThread;
begin
Timer1.Enabled := False;

UpThread := UploadThread.Create(true);
UpThread.Create;
UpThread.Resume;

end;

end.

线程经过:

unit ThreadUpload;

interface

uses
Classes, UploaderForm;

type
UploadThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;

implementation

{ UploadThread }

procedure UploadThread.Execute;
begin
With FormUploader do
begin
imgUpdate.Picture := imgRunning.Picture;
end;
end;

end.

我无法解决这个问题。

最佳答案

TThread.Synchronize() 默认情况下在 DLL 中不起作用,因为 Synchronize() 发布到的同步队列对于调用它的可执行文件来说是本地的。换句话说,当应用程序调用 Synchronize() 时,它会发布到 exe 文件本地的队列。当 DLL 调用 Synchronize() 时,它会发布到 dll 文件本地的队列。当应用程序在空闲时间泵送其同步队列时,它不会自动泵送 DLL 的队列。您必须从 DLL 导出一个函数,然后应用程序可以在需要时调用该函数,例如在 TApplication.OnIdle 事件或计时器中。然后,导出的函数可以调用 RTL 的 CheckSynchronize() 函数来泵送 DLL 的同步队列。

关于multithreading - delphi中的DLL、表单和线程(合一)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5792573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com