delphi多线程?Delphi怎样实现多线程
朋友们,delphi多线程和Delphi怎样实现多线程是当今热门话题,但是它们的内涵和影响力可能会让人感到困惑。在本篇文章中,我将为你们揭示它们的本质和重要性,希望能够为你们带来新的认识。
Delphi怎样实现多线程
多线程其实非常简单,DELPHI有多线程的类,你添加即可使用,但是我喜欢自己调用API,方法如下:
定义一个函数或者过程,这个函数就是线程要执行的内容,然后调用API就可以不断创建线程,每个线程都单独的执行那个函数,执行完毕线程就自动关闭,下面是我程序里面的部分代码:
{下面这个就是线程过程,我的线程传递一个参数,建立的SOCKET}
procedure ClientThread(var sock:TSOCKET); stdcall;
var……;
begin
……
end;
{下面是主程序建立服务,等待连接,连接后调用线程进行处理的代码}
repeat
iAddrSize:= sizeof(client);
sClient:= accept(sListen,@client,@iAddrSize);
if sClient=INVALID_SOCKET then
begin
SocketErrorMsg(sClient,'accept() fail');
break;
end;
writeln('Accepted client:',inet_ntoa(client.sin_addr),':',ntohs(client.sin_port),' sock=',sClient);
hThread:= CreateThread(nil,$1000,@ClientThread,@sClient, 0, dwThreadId);
if hThread=0 then
begin
writeln('CreateThread() fail:',GetLastError);
break;
end;
CloseHandle(hThread);
until false;
delphi 怎么实现多线程的同步
多线程同步
"临界区"(CriticalSection):当把一段代码放入一个临界区,线程执行到临界区时就独占了,让其他也要执行此代码的线程先等等;
使用格式如下:
varCS:TRTLCriticalSection;{声明一个TRTLCriticalSection结构类型变量;它应该是全局的}
InitializeCriticalSection(CS);{初始化}
EnterCriticalSection(CS);{开始:轮到我了其他线程走开}
LeaveCriticalSection(CS);{结束:其他线程可以来了}
DeleteCriticalSection(CS);{删除:注意不能过早删除}
多线程同步示例
1unitUnit1;
2
3interface
4
5uses
6Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
7Dialogs,StdCtrls;
8
9type
10TForm1=class(TForm)
11ListBox1:TListBox;
12Button1:TButton;
13procedureFormCreate(Sender:TObject);
14procedureFormDestroy(Sender:TObject);
15procedureButton1Click(Sender:TObject);
16end;
17
18var
19Form1:TForm1;
20
21implementation
22
23{$R*.dfm}
24
25var
26CS:TRTLCriticalSection;
27
28functionMyThreadFun(p:Pointer):DWORD;stdcall;
29var
30i:Integer;
31begin
32EnterCriticalSection(CS);
33fori:=0to99doForm1.ListBox1.Items.Add(IntToStr(i));
34LeaveCriticalSection(CS);
35Result:=0;
36end;
37
38procedureTForm1.Button1Click(Sender:TObject);
39var
40ID:DWORD;
41begin
42CreateThread(nil,0,@MyThreadFun,nil,0,ID);
43CreateThread(nil,0,@MyThreadFun,nil,0,ID);
44CreateThread(nil,0,@MyThreadFun,nil,0,ID);
45end;
46
47procedureTForm1.FormCreate(Sender:TObject);
48begin
49ListBox1.Align:=alLeft;
50InitializeCriticalSection(CS);
51end;
52
53procedureTForm1.FormDestroy(Sender:TObject);
54begin
55DeleteCriticalSection(CS);
56end;
57
58end.
delphi多线程问题
你是用一个统一的任务列表来管理任务吗?实际线程自身是不应该去读任务列表里的任务的,应该由任务列表来创建执行当前任务所需要的线程。线程执行结束后,用Synchronize方法来通知任务列表,这个线程执行完了。那任务列表去提取下一个任务,直到所有任务都执行完。
Delphi多线程问题
{注意哦,你的两个线程用的同一个变量装它们的句柄,这样是不对地,出错地原因是因为循环操作可视控件的时候,如果不排队,会出现同时访问的情况,最好是可以使用类的方式来使用线程}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1= class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
// procedure FormCreate(Sender: TObject);
private
{ Private declarations}
public
{ Public declarations}
end;
var
Form1: TForm1;
hloopHandle:Thandle;//线程句柄
dloopThreadID:DWORD;//线程 id
//================
lpHandles:Thandle;
lpTheradID:DWORD;
function doloop(P:pointer):Longint;stdcall;
Function StartEvent(S:pointer):Longint;stdcall;
implementation
{$R*.dfm}
//按钮一
procedure TForm1.Button1Click(Sender: TObject);
begin
hloopHandle:= CreateThread(nil,0,@doloop,nil,0,dloopThreadID);
if hloopHandle=0 then
begin
messagebox(Handle,'Didn’t Create a Thread',nil,MB_OK);
abort;
end;
end;
//按钮二
procedure TForm1.Button2Click(Sender: TObject);
begin
lpHandles:= CreateThread(nil,0,@StartEvent,nil,0,lpTheradID);
if lpHandles=0 then
begin
messagebox(Handle,'Didn’t Create a Thread',nil,MB_OK);
abort;
end;
end;
//线程处理函数一
function doloop(P:pointer):integer;stdcall;
var
i:integer;
begin
for i:=0 to 100000 do
begin
form1.Edit1.Text:=inttostr(i);
sleep(100);
end;
end;
//==================================
//线程处理函数二
Function StartEvent(S:pointer):integer;stdcall;
var
c:integer;
begin
for c:=0 to 100000 do
begin
form1.Edit2.Text:=inttostr(c);
sleep(100);
end;
end;
end.
好了,本文到此结束,如果可以帮助到大家,还望关注本站哦!