同步怎么会这样?(100分)

  • 主题发起人 主题发起人 huiyue
  • 开始时间 开始时间
H

huiyue

Unregistered / Unconfirmed
GUEST, unregistred user!
我用
SHandle :=CreateThread(nil,0,@TAnalyseSimulation.LoopQuery,nil,0,ThreadID);
创建了线程,但是同步的时候却遇到一个不可理喻的问题。
procedure TAnalyseSimulation.LoopQuery;
begin
Synchronize(adoquery1exe);
AnalyseSimulation.ADOQuery1.Requery;
end;
其中adoquery1exe是
procedure TAnalyseSimulation.adoquery1exe;
begin
//。。。。。。。
end;

[Error] AnalyseSimulation_p.pas(83): Missing operator or semicolon
[Error] AnalyseSimulation_p.pas(83): Statement expected, but expression of type 'Integer' found
 
怎么,没有人能解吗?
我是觉得这个问题太怪。不知道怎么回事,语法上面我看不出错误。
可就是编译出错,怪死了。
 
你自己创建线程好像不能用Synchronize,要用的话用TThread类创建
否自用Mutex就行了,给你个例子
unit main;
interface
uses
Windows, Messages, SysUtils,
Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls,
Menus;
const
TotalCount = 20;
type
TCommRec = record
CommName:string;
//串口号
CommHandle:THandle;
//串口句柄
end;

TForm1 = class(TForm)
ListBox1: TListBox;
ListBox2: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
{ Private declarations }
private
{ Private declarations }
public
procedure WriteTime(Str:String);
{ Public declarations }
end;

var
Form1: TForm1;
// CommArray:array of TCommRec;
implementation
{$R *.DFM}
var
hMutex: THandle;
GlobalData: Integer;
const gunnum=10;

function ThreadFunc1(P: Pointer): LongInt;
stdcall;
var
i: Integer;
begin
Form1.ListBox1.Clear;
for i := 0 to gunnum-1do
begin
// Form1.GetCommHandle('线程1'+TimeToStr(now));
Form1.ListBox1.addItem(timetostr(now),nil);
Form1.WriteTime('线程1');
sleep(10);
end;

Result := 0;
end;

function ThreadFunc2(P: Pointer): LongInt;
stdcall;
var
i: Integer;
begin
Form1.ListBox2.Clear;
for i := 0 to gunnum-1do
begin
// Form1.GetCommHandle('线程2'+TimeToStr(now));
Form1.WriteTime('线程2');
Form1.ListBox2.AddItem(timetostr(now),nil);
sleep(10);
end;

Result := 0;
end;

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

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

procedure TForm1.Button1Click(Sender: TObject);
var
ThreadID1: DWord;
ThreadID2: DWord;
ThreadHandles: array [0..1] of THandle;
begin
GlobalData := 100;
ThreadHandles[0] := CreateThread(nil, 0, @ThreadFunc1,
nil, 0, ThreadID1);
ThreadHandles[1] := CreateThread(nil, 0, @ThreadFunc2,
nil, 0, ThreadID2);
if (ThreadHandles[0] = 0) or (ThreadHandles[1] = 0) then
MessageBox(Handle, 'No Thread', nil, MB_OK);
end;

procedure TForm1.WriteTime(Str: String);
var fileName,logstr:string;
f:textfile;
begin
if WaitForSingleObject(hMutex,Infinite)=Wait_Object_0 then
begin
fileName:=ExtractFilepath(application.ExeName)+'1.txt';
system.Assign(F,fileName);
if not FileExists(fileName) then
rewrite(F)
else
Append(F);
logstr:=Str+TimeToStr(Now)+#13+#10;
writeln(F,logstr);
CloseFile(F);
ReleaseMutex(hMutex);
end;
end;

end.
 
wfzha is right
 
多人接受答案了。
 
后退
顶部