多线程求助。

zjxsw 2010-08-13 01:54:28
function vfun(ir: Pointer): integer; stdcall;
var
k, t, m, i,lk: integer;
ss: string;
lpwd, luserid, pass: pchar;

begin


for m := 1 to 100 do
begin

ss := inttostr(m);// 程序发生异常,注释此句就正常,什么原因啊。

end;



end;

function MyThreadFun(p: Pointer): Integer; stdcall;

begin
vfun(p);
Result := 0;


end;



procedure TForm1.Button1Click(Sender: TObject);
var
ID: DWORD;
st: array[0..9] of integer;
ii: integer;
begin


for ii := 0 to 9 do
begin
st[ii] := ii;
hThread[ii] := CreateThread(nil, 0, @MyThreadFun, @st[ii], 0, ID);
end;
// Button1.Enabled := False;
end;
...全文
138 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjxsw 2010-08-13
  • 打赏
  • 举报
回复
又不行了,线程里调用某dll里的一个函数。程序只能启动一个线程了。郁闷了。

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
var
CS:TRTLCriticalSection;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//定义线程类
type
tyThread = class(TThread)
private
Str: string;
i1,i2:integer;
protected

procedure showLabel;
procedure Execute; override;
public
LLabel: TLabel;
end;

var
Form1: TForm1;
SArrayLabel: Array [0..1] of TLabel;

implementation

{$R *.dfm}
//线程类实现部分

procedure tyThread.Execute;
var
m: integer;

begin
//EnterCriticalSection(Cs); //进入临界段

for m := i1 to i2 do
begin
sleep(5);
str := inttostr(m);
synchronize(showLabel); //同步访问VCL可视组件

此处调用dll函数,程序只能启动了一个线程了。
end;
//LeaveCriticalSection(Cs); //退出临界段


end;

procedure TyThread.showLabel;
begin
LLabel.caption:=str;
//Application.ProcessMessages;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
th:array[0..1] of TyThread;
ii:integer;
begin
//Listbox1.Clear;
//Listbox2.Clear;
for ii:= 0 to 1 do
begin
th[ii] := tyThread.Create(true); //创建线程1
th[ii].LLabel := SArrayLabel[ii];
th[ii].i1:=ii*1000;
th[ii].i2:=ii*1000+1000;
th[ii].Resume; //开始执行

end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
InitializeCriticalSection(Cs);
SArrayLabel[0] := Label1;
SArrayLabel[1] := Label2;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
DeleteCriticalSection(Cs) ;

end;

end.

zjxsw 2010-08-13
  • 打赏
  • 举报
回复
终于模仿出来了,还真麻烦。

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
var
CS:TRTLCriticalSection;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//定义线程类
type
tyThread = class(TThread)
private
Str: string;
i1,i2:integer;
protected

procedure showLabel;
procedure Execute; override;
public
LLabel: TLabel;
end;

var
Form1: TForm1;
SArrayLabel: Array [0..1] of TLabel;

implementation

{$R *.dfm}
//线程类实现部分

procedure tyThread.Execute;
var
m: integer;

begin
//EnterCriticalSection(Cs); //进入临界段

for m := i1 to i2 do
begin
sleep(5);
str := inttostr(m);
synchronize(showLabel); //同步访问VCL可视组件
end;
//LeaveCriticalSection(Cs); //退出临界段


end;

procedure TyThread.showLabel;
begin
LLabel.caption:=str;
//Application.ProcessMessages;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
th:array[0..1] of TyThread;
ii:integer;
begin
//Listbox1.Clear;
//Listbox2.Clear;
for ii:= 0 to 1 do
begin
th[ii] := tyThread.Create(true); //创建线程1
th[ii].LLabel := SArrayLabel[ii];
th[ii].i1:=ii*1000;
th[ii].i2:=ii*1000+1000;
th[ii].Resume; //开始执行

end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
InitializeCriticalSection(Cs);
SArrayLabel[0] := Label1;
SArrayLabel[1] := Label2;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
DeleteCriticalSection(Cs) ;

end;

end.

zjxsw 2010-08-13
  • 打赏
  • 举报
回复
哪位好心人帮我写个示例代码吧。
功能如下,一个按钮,两个label。使用多线程,使一个label显示1 to 1000,同时使另一个label显示1000 to 2000

头疼死了,高手帮帮忙啊。
pathletboy 2010-08-13
  • 打赏
  • 举报
回复
工程文件里加
IsMultiThread := True;

像这样:
program Project1;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
IsMultiThread := True;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
zjxsw 2010-08-13
  • 打赏
  • 举报
回复
谢LSD,还是老样子啊,郁闷。
Blessed_Chuan 2010-08-13
  • 打赏
  • 举报
回复

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
hMutex:THandle;
implementation

{$R *.dfm}
function vfun(ir: Pointer): integer; stdcall;
var
k, t, m, i,lk: integer;
ss: string;
lpwd, luserid, pass: pchar;
begin
for m := 1 to 100 do
begin
ss := inttostr(m);// 程序发生异常,注释此句就正常,什么原因啊。
end;
end;

function MyThreadFun(p: Pointer): Integer; stdcall;
begin
vfun(p);
Result := 0;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
CloseHandle(hMutex);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
hMutex:=CreateMutex(nil,false,nil);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
ID: DWORD;
st: array[0..9] of integer;
hThread: array[0..9] of integer;
ii: integer;
begin
for ii := 0 to 9 do
begin
WaitForSingleObject(hMutex,INFINITE) ;
st[ii] := ii;
hThread[ii] := CreateThread(nil, 0, @MyThreadFun, @st[ii], 0, ID);
ReleaseMutex(hMutex);
end;
// Button1.Enabled := False;
end;


end.
zjxsw 2010-08-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lqfcu2 的回复:]
看看线程同步的相关知识吧。。
[/Quote]

能不能帮改改代码啊,没有头绪啊。谢谢。
lqfcu2 2010-08-13
  • 打赏
  • 举报
回复
看看线程同步的相关知识吧。。
zjxsw 2010-08-13
  • 打赏
  • 举报
回复
怎么没人来啊。
我增加了10个相同的函数代码,程序没出错。

求救啊。。。
zjxsw 2010-08-13
  • 打赏
  • 举报
回复
程序用意:
开10个线程,每个线程调用同一个函数。函数功能,比方从1加到100 (第一个线程从1加到100、第二个线程100加到200。。。。。)
zjxsw 2010-08-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]
要建立互斥
[/Quote]
怎么弄啊,请详解,我是新手。
bdmh 2010-08-13
  • 打赏
  • 举报
回复
要建立互斥

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧