在子线程中执行主线程中的过程,和在子线程中执行同样的过程, 找仓库管理
销售管理软件版1楼: 比如,在主线程中的代码
procedure TForm1.GetData;
var
smMsg:TSMMSG;
begin
with ADOQuery1 do
begin
close;
sql.Clear;
sqlstr:=''select autono,sourceNumber,destiNumber,SMcontent,SerialNo,Status from T_SendingSM where status is null'';
sql.add(sqlstr);
open;
smMsg.Msg := WM_SENDSM;
if RecordCount<>0 then
begin
while not eof do
begin
application.ProcessMessages ;
smMsg.SourceNumber:=FieldByName(''sourceNumber'').asstring;
smMsg.DestinationNumber:=FieldByName(''destiNumber'').asstring;
smMsg.SMContent:=FieldByName(''SMcontent'').asstring;
smMsg.SMSerialNo := ProduceSerial; // 取得短消息序列号
edit;
application.ProcessMessages ;
FieldByName(''SerialNo'').AsInteger :=smMsg.SMSerialNo ;
FieldByName(''Status'').AsString :=''sending'';
Form1.Dispatch(smMsg) ;
application.ProcessMessages ;
sleep(SmppSrvInfo.SendSM_Interval);
next;
end;
edit;
Post ;
end;
close;
end;
end;
在子线程中调用它
procedure TGetData.Execute;
begin
CoInitialize(nil);
{ Place thread code here }
inherited FreeOnTerminate := TRUE;
Form1.GetData;
end;
这样做和在子线程中实现同样的过程有区别吗?
procedure TForm1.GetData;
var
smMsg:TSMMSG;
begin
with ADOQuery1 do
begin
close;
sql.Clear;
sqlstr:=''select autono,sourceNumber,destiNumber,SMcontent,SerialNo,Status from T_SendingSM where status is null'';
sql.add(sqlstr);
open;
smMsg.Msg := WM_SENDSM;
if RecordCount<>0 then
begin
while not eof do
begin
application.ProcessMessages ;
smMsg.SourceNumber:=FieldByName(''sourceNumber'').asstring;
smMsg.DestinationNumber:=FieldByName(''destiNumber'').asstring;
smMsg.SMContent:=FieldByName(''SMcontent'').asstring;
smMsg.SMSerialNo := ProduceSerial; // 取得短消息序列号
edit;
application.ProcessMessages ;
FieldByName(''SerialNo'').AsInteger :=smMsg.SMSerialNo ;
FieldByName(''Status'').AsString :=''sending'';
Form1.Dispatch(smMsg) ;
application.ProcessMessages ;
sleep(SmppSrvInfo.SendSM_Interval);
next;
end;
edit;
Post ;
end;
close;
end;
end;
在子线程中调用它
procedure TGetData.Execute;
begin
CoInitialize(nil);
{ Place thread code here }
inherited FreeOnTerminate := TRUE;
Form1.GetData;
end;
这样做和在子线程中实现同样的过程有区别吗?
2楼: 没有区别,不过和线程中一样,需要考虑线程同步。尽管那个过程在主模块中,但执行其实在线程中的。 如仓库管理
3楼: 包括主线程中的组件也是一样吗?
4楼: 一样。包括组件更加需要线程同步了。
5楼: 引用了主窗体的组件,需要考虑同步!
6楼: 代码是静态的。线程是程序的一条执行路径,是动态的,它可能贯穿不同类(当然也包括线程类窗体类什么的)的代码。