我前段时间已经看到人们在Delphi XE中讨论新的多线程,以及由于Delphi实现多线程的方式,Delphi存在一些"原生"问题.他们建议使用一些外部库来替换默认的Delphi多线程.
你能指点我一些文档和Delphi XE最流行的多线程库吗?谢谢
之前有人问过,但没有完整答案.这与所谓的着名"'致命线程模型!'"有关.
我需要用安全的东西替换这个调用TThread.Suspend,当终止或恢复时返回:
procedure TMyThread.Execute;
begin
while (not Terminated) do begin
if PendingOffline then begin
PendingOffline := false; // flag off.
ReleaseResources;
Self.Suspend; // suspend thread. { evil! ask Barry Kelly why.}
// -- somewhere else, after a long time, a user clicks
// a resume button, and the thread resumes: --
if Terminated then
exit; // leave TThread.Execute.
// Not terminated, so we continue..
GrabResources;
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
最初的答案含糊地暗示了"TMutex,TEvent和关键部分".
我想我正在寻找一个TThreadThatDoesntSuck.
以下是带有Win32Event的TThread派生示例,供评论:
unit SignalThreadUnit;
interface
uses
Classes,SysUtils,Windows;
type
TSignalThread …Run Code Online (Sandbox Code Playgroud) 我之前从未使用过线程,现在我正在尝试创建一个带有查询的线程来检查数据库状态.查询如下:
select (*) as DBCount from v$datafile where status in 'OFFLINE';.
此查询返回脱机的所有数据库的总数.现在我想在Delphi中创建一个线程,当我运行它并在标签上显示结果时,它将在我的应用程序的后台执行此查询.
我是多线程的新手,但不是一个完整的新手.我需要在工作线程中执行对Web服务的调用.
在主线程中,我有一个表单(TForm),它有一个私有数据成员(私有字符串),只有工作线程才能写入(我在它恢复之前将指针传递给线程).当工作线程完成其webservice调用并将结果响应xml写入表单上的私有成员时,工作线程使用PostMessage将消息发送到表单的句柄(我在恢复之前也将其传递给线程).
interface
const WM_WEBSERVCALL_COMPLETE = WM_USER + 1;
type
TWebServiceResponseXML = string;
PWebServiceResponseXML = ^TWebServiceResponseXML;
TMyForm = class(TForm)
...
private
...
fWorkerThreadID: Cardinal;
fWebServiceResponseXML: TWebServiceResponseXML;
public
...
procedure StartWorkerThread;
procedure OnWebServiceCallComplete(var Message: TMessage); Message WM_WEBSERVCALL_COMPLETE;
end;
TMyThread = class(TThread)
private
protected
procedure Execute; override;
public
SenderHandle: HWnd;
RequestXML: string;
ResponseXML: string;
IMyService: IService;
PResponseXML: PWebServiceResponseXML;
end;
implementation
procedure TMyForm.StartWorkerThread;
var
MyWorkerThread: TMyThread;
begin
MyWorkerThread := TMyThread.Create(True);
MyWorkerThread.FreeOnTerminate := True;
MyWorkerThread.SenderHandle := self.Handle;
MyWorkerThread.RequestXML := ComposeRequestXML;
MyWorkerThread.PResponseXML := ^fWebServiceResponseXML;
MyWorkerThread.Resume; …Run Code Online (Sandbox Code Playgroud) 我现在有一个几乎完成的应用程序,我想要实现的下一个功能是线程.我选择使用BeginThread(),虽然我知道delphi中的TThread.我遇到的问题是BeginThread()调用的结构.通常,程序中调用我想要线程化的函数的行是
CompareFiles(form1.Edit3.Text,Form1.Edit4.Text,Form1.StringGrid2,op);
Run Code Online (Sandbox Code Playgroud)
op是一个整数.
我已经将它切换出来以便从中创建一个线程
BeginThread(nil,0,CompareFiles,Addr('form1.Edit3.Text,Form1.Edit4.Text,Form1.StringGrid2,op'),0,x);
Run Code Online (Sandbox Code Playgroud)
从我可以找到的关于如何实际使用BeginThread()的少量信息来看,这应该是一个很好的调用,但是在编译时我得到的是关于我的BeginThread()语句参数的结构的编译器错误.
编辑信息.
调用CompareFiles的当前过程是
procedure TForm1.Panel29Click(Sender: TObject);
var
op,x : integer;
begin
if (Form1.Edit3.Text <> '') AND (Form1.Edit4.Text <> '') then
begin
op := 3;
if RadioButton7.Checked = True then op := 0;
if RadioButton3.Checked = True then op := 1;
if RadioButton4.Checked = True then op := 2;
if RadioButton5.Checked = True then op := 3;
if RadioButton6.Checked = True then op := 4;
CompareFiles(form1.Edit3.Text,Form1.Edit4.Text,Form1.StringGrid2,op);
end;
end;
Run Code Online (Sandbox Code Playgroud)
如果我按照几个人的建议使用TThread,并且如下面Rob所示,我很困惑a)我将如何将op,Edit3/4.Text和StringGrid2传递给CompareFiles.从TThread的例子猜测我已经看过我以为我会替换上面的代码TCompareFilesThread.Execute并将当前代码从Panel29Click放入TCompareFilesThread.Create然后添加
FEdit3Text := Edit3Text; …Run Code Online (Sandbox Code Playgroud) 我有一个大的二进制文件(大约700 Mb),我加载到TMemoryStream.之后,我使用TMemoryStream.Read()执行读取并进行一些简单的计算,但应用程序永远不会占用超过20%的CPU.我的电脑有i7处理器.有没有机会在不使用线程的情况下增加CPU使用并加快读取过程?
我正在寻找一些线程框架,以避免从划痕中写出来.特别是排队和同步的任务是我所需要的.我知道OmniThreadLibrary很棒,但与D7不兼容.有什么建议?
请问你能帮帮我吗.我正在编写一个组件(类似于TListView),在我的组件中,我逐个执行3个过程:
procedure findFiles(Path:String); // using FindFirst, FindNext
procedure ArrangeItems; // assigns Item Position
procedure SetIcons; // Loads Images, resizes, adds to ImageList
Run Code Online (Sandbox Code Playgroud)
我无法理解如何使我的组件使用Threads来执行所有这些过程,至少是第一个(findFiles).我尝试了不同的方法但没有任何结果.
这是我所拥有的一切(只是一个基本的例子).
type
TSearchThread = class(TThread)
private
SIView: TSIView;
protected
procedure Execute; override;
procedure DoSearch;
public
constructor Create(fSIView: TSIView);
end;
constructor TSearchThread.Create(fSIView: TSIView);
begin
SIView := fSIView;
FreeOnTerminate := True;
inherited Create(False);
Priority := tpLower;
end;
procedure TSearchThread.Execute;
begin
inherited;
Synchronize(DoSearch);
end;
first I tried to perform
SIView.findFiles('C:\');
ArrangeItems;
Run Code Online (Sandbox Code Playgroud)
然后才执行线程来设置适当的图标(SetIcons):
procedure TSearchThread.DoSearch;
begin
SetIcons;
end;
Run Code Online (Sandbox Code Playgroud)
它有效,但有错误:有时我的组件创建的图标不是所有项目,有时一些项目的图标完全填充黑色,有时我根本没有图标;
然后我决定在线程中执行所有3个程序:
procedure …Run Code Online (Sandbox Code Playgroud) delphi ×9
tthread ×2
beginthread ×1
delphi-7 ×1
delphi-xe ×1
delphi-xe2 ×1
firemonkey ×1
preemptive ×1
task ×1