源代码:
unit DrawShipTrack;interfaceuses
Classes,MapXLib_TLB,Variants,Activex,MapCommfunc,SysUtils,Dialogs, ShowTrack;type
pTrackInfo=^TTrackInfo;
TTrackInfo=Record
TerminalNo: string;
ShipName: string;
Speed: Double;
Direct: Double;
MapX: Double;
Mapy: Double;
sDateTime: String;
end;
TDrawShipTrack = class(TThread)
private
{ Private declarations }
fArrayTrackInfo: Array of pTrackInfo;
fMap: TMap;
fArrayCount: integer;
CurrentList: integer;
m_PolygonID: integer;
m_PointID: integer;
destructor Destroy;
protected
procedure Execute; override;
procedure WriteLog(msg: string);
public
fCommand: String;
procedure setMainForm;
constructor Create(Map: TMap; Command: string; ArrayTrackInfo: pointer; ArrayCount: integer);
procedure DrawLine(TerminalNo: String; ShipName: String; Speed: Double; Direct: Double; Lang: Double; Lat: Double; sDateTime: string);
procedure DelPoint(TerminalNo: String; ShipName: String; Speed: Double; Direct: Double; Lang: Double; Lat: Double; sDateTime: string); end;implementation
uses Main;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TDrawShipTrack.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TDrawShipTrack }
constructor TDrawShipTrack.Create(Map: TMap; Command: string; ArrayTrackInfo:pointer; ArrayCount: integer) ;
begin
fCommand:=Command;
fMap:=Map;
fArrayCount:=ArrayCount;
SetLength(fArrayTrackInfo,fArrayCount);
fArrayTrackInfo:=ArrayTrackInfo; inherited Create(False);
end;procedure TDrawShipTrack.Execute;
var
i: integer;
TerminalNo: String;
ShipName: string;
Speed: Double;
Direct: Double;
Lang: Double;
Lat: Double;
sDateTime: String;
begin
{ Place thread code here }
FreeOnTerminate:=True;
CurrentList:=0;
while not Terminated do
begin
try
if fCommand='PLAY' then
begin
TerminalNo:=fArrayTrackInfo[CurrentList]^.TerminalNo ;
ShipName:=fArrayTrackInfo[CurrentList]^.ShipName ;
Speed:=fArrayTrackInfo[CurrentList]^.Speed;
Direct:=fArrayTrackInfo[CurrentList]^.Direct;
Lang:=fArrayTrackInfo[CurrentList]^.MapX;
Lat:=fArrayTrackInfo[CurrentList]^.Mapy;
sDateTime:=fArrayTrackInfo[CurrentList]^.sDateTime;
DrawLine(TerminalNo,ShipName,Speed,Direct,Lang,Lat,sDateTime);
CurrentList:=CurrentList + 1;
Synchronize(setMainForm);
end;
if fCommand='GO' then
begin
if CurrentList<fArrayCount then
begin
CurrentList:=CurrentList + 1;
TerminalNo:=fArrayTrackInfo[CurrentList]^.TerminalNo ;
ShipName:=fArrayTrackInfo[CurrentList]^.ShipName ;
Speed:=fArrayTrackInfo[CurrentList]^.Speed;
Direct:=fArrayTrackInfo[CurrentList]^.Direct;
Lang:=fArrayTrackInfo[CurrentList]^.MapX;
Lat:=fArrayTrackInfo[CurrentList]^.Mapy;
sDateTime:=fArrayTrackInfo[CurrentList]^.sDateTime;
DrawLine(TerminalNo,ShipName,Speed,Direct,Lang,Lat,sDateTime);
Synchronize(setMainForm);
Suspend;
end;
end;
if fCommand='BACK' then
begin
if CurrentList>1 then
begin
CurrentList:=CurrentList - 1;
TerminalNo:=fArrayTrackInfo[CurrentList]^.TerminalNo ;
ShipName:=fArrayTrackInfo[CurrentList]^.ShipName ;
Speed:=fArrayTrackInfo[CurrentList]^.Speed;
Direct:=fArrayTrackInfo[CurrentList]^.Direct;
Lang:=fArrayTrackInfo[CurrentList]^.MapX;
Lat:=fArrayTrackInfo[CurrentList]^.Mapy;
sDateTime:=fArrayTrackInfo[CurrentList]^.sDateTime;
DelPoint(TerminalNo,ShipName,Speed,Direct,Lang,Lat,sDateTime);
Synchronize(setMainForm);
Suspend;
end;
end;
sleep(1000);
if CurrentList=fArrayCount then
begin
Synchronize(setMainForm);
Terminate;
end;
if Terminated then exit;
except on e:exception do
writelog('Excute 中错误 ' + e.Message);
//showmessage('程序运行时发生错误:' + e.Message);
end;
end;
end;procedure TDrawShipTrack.setMainForm ;
begin
frmMain.TrackBar.Position:=trunc((CurrentList*10/fArrayCount) + 0.5);
if CurrentList=fArrayCount then
begin
frmMain.btnPlay.Enabled :=True;
frmMain.btnPause.Enabled :=False;
frmMain.btnContiue.Enabled :=False;
frmMain.btnGO.Enabled :=False;
frmMain.btnBack.Enabled :=False;
frmMain.btnStop.Enabled :=False;
end;
frmMain.ShipTrackList.Items[CurrentList-1].Selected :=True;
frmMain.ShipTrackList.Items[CurrentList-1].MakeVisible(True);
end;procedure TDrawShipTrack.DrawLine(TerminalNo: String; ShipName: String; Speed: Double; Direct: Double; Lang: Double; Lat: Double; sDateTime: string);
var
m_points: CMapxPoints;
m_Point: CMapxPoint;
m_Polygon: Variant;
m_Layer: CMapxLayer;
m_PointStyle: CMapxStyle;
m_LineStyle: CMapxStyle;
m_NewSymbol: Variant;
m_NewLine: Variant;
m_LineFeature: Variant;
m_PointFeature: Variant;
unusedVt: OleVariant;
begin
try
....
except on e:exception do
writelog('DrawLine 中错误 ' + e.Message);
//showmessage('程序运行时发生错误:' + e.Message );
end;
end;procedure TDrawShipTrack.DelPoint(TerminalNo: String; ShipName: String; Speed: Double; Direct: Double; Lang: Double; Lat: Double; sDateTime: string);
var
m_pointFeature: Variant;
m_LineFeature: Variant;
m_LineStyle: CMapxStyle;
begin
try
......
except on e:exception do
writelog('DelPoint 中错误 ' + e.Message);
//showmessage('程序运行时发生错误:' + e.Message);
end;
end;
destructor TDrawShipTrack.Destroy;
begin
try
CoUninitialize();
fMap:=nil;
fArrayTrackInfo:=nil;
inherited destroy;
except on e:exception do
writelog('Destroy 中错误 ' + e.Message);
end;
end;
procedure TDrawShipTrack.WriteLog(msg: string);
var
sFile: textFile;
begin
Assignfile(sfile,'login.txt');
Append(sFile);
writeln(sfile,msg);
closefile(sFile);
end;end.此线程执行完成后,主程序退出时,发生内存读取错误.如果主程序不启动此线程,则退出主程序时不会发生内存读取错误.
麻烦各位高手帮我看看,出出主意.
unit DrawShipTrack;interfaceuses
Classes,MapXLib_TLB,Variants,Activex,MapCommfunc,SysUtils,Dialogs, ShowTrack;type
pTrackInfo=^TTrackInfo;
TTrackInfo=Record
TerminalNo: string;
ShipName: string;
Speed: Double;
Direct: Double;
MapX: Double;
Mapy: Double;
sDateTime: String;
end;
TDrawShipTrack = class(TThread)
private
{ Private declarations }
fArrayTrackInfo: Array of pTrackInfo;
fMap: TMap;
fArrayCount: integer;
CurrentList: integer;
m_PolygonID: integer;
m_PointID: integer;
destructor Destroy;
protected
procedure Execute; override;
procedure WriteLog(msg: string);
public
fCommand: String;
procedure setMainForm;
constructor Create(Map: TMap; Command: string; ArrayTrackInfo: pointer; ArrayCount: integer);
procedure DrawLine(TerminalNo: String; ShipName: String; Speed: Double; Direct: Double; Lang: Double; Lat: Double; sDateTime: string);
procedure DelPoint(TerminalNo: String; ShipName: String; Speed: Double; Direct: Double; Lang: Double; Lat: Double; sDateTime: string); end;implementation
uses Main;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TDrawShipTrack.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TDrawShipTrack }
constructor TDrawShipTrack.Create(Map: TMap; Command: string; ArrayTrackInfo:pointer; ArrayCount: integer) ;
begin
fCommand:=Command;
fMap:=Map;
fArrayCount:=ArrayCount;
SetLength(fArrayTrackInfo,fArrayCount);
fArrayTrackInfo:=ArrayTrackInfo; inherited Create(False);
end;procedure TDrawShipTrack.Execute;
var
i: integer;
TerminalNo: String;
ShipName: string;
Speed: Double;
Direct: Double;
Lang: Double;
Lat: Double;
sDateTime: String;
begin
{ Place thread code here }
FreeOnTerminate:=True;
CurrentList:=0;
while not Terminated do
begin
try
if fCommand='PLAY' then
begin
TerminalNo:=fArrayTrackInfo[CurrentList]^.TerminalNo ;
ShipName:=fArrayTrackInfo[CurrentList]^.ShipName ;
Speed:=fArrayTrackInfo[CurrentList]^.Speed;
Direct:=fArrayTrackInfo[CurrentList]^.Direct;
Lang:=fArrayTrackInfo[CurrentList]^.MapX;
Lat:=fArrayTrackInfo[CurrentList]^.Mapy;
sDateTime:=fArrayTrackInfo[CurrentList]^.sDateTime;
DrawLine(TerminalNo,ShipName,Speed,Direct,Lang,Lat,sDateTime);
CurrentList:=CurrentList + 1;
Synchronize(setMainForm);
end;
if fCommand='GO' then
begin
if CurrentList<fArrayCount then
begin
CurrentList:=CurrentList + 1;
TerminalNo:=fArrayTrackInfo[CurrentList]^.TerminalNo ;
ShipName:=fArrayTrackInfo[CurrentList]^.ShipName ;
Speed:=fArrayTrackInfo[CurrentList]^.Speed;
Direct:=fArrayTrackInfo[CurrentList]^.Direct;
Lang:=fArrayTrackInfo[CurrentList]^.MapX;
Lat:=fArrayTrackInfo[CurrentList]^.Mapy;
sDateTime:=fArrayTrackInfo[CurrentList]^.sDateTime;
DrawLine(TerminalNo,ShipName,Speed,Direct,Lang,Lat,sDateTime);
Synchronize(setMainForm);
Suspend;
end;
end;
if fCommand='BACK' then
begin
if CurrentList>1 then
begin
CurrentList:=CurrentList - 1;
TerminalNo:=fArrayTrackInfo[CurrentList]^.TerminalNo ;
ShipName:=fArrayTrackInfo[CurrentList]^.ShipName ;
Speed:=fArrayTrackInfo[CurrentList]^.Speed;
Direct:=fArrayTrackInfo[CurrentList]^.Direct;
Lang:=fArrayTrackInfo[CurrentList]^.MapX;
Lat:=fArrayTrackInfo[CurrentList]^.Mapy;
sDateTime:=fArrayTrackInfo[CurrentList]^.sDateTime;
DelPoint(TerminalNo,ShipName,Speed,Direct,Lang,Lat,sDateTime);
Synchronize(setMainForm);
Suspend;
end;
end;
sleep(1000);
if CurrentList=fArrayCount then
begin
Synchronize(setMainForm);
Terminate;
end;
if Terminated then exit;
except on e:exception do
writelog('Excute 中错误 ' + e.Message);
//showmessage('程序运行时发生错误:' + e.Message);
end;
end;
end;procedure TDrawShipTrack.setMainForm ;
begin
frmMain.TrackBar.Position:=trunc((CurrentList*10/fArrayCount) + 0.5);
if CurrentList=fArrayCount then
begin
frmMain.btnPlay.Enabled :=True;
frmMain.btnPause.Enabled :=False;
frmMain.btnContiue.Enabled :=False;
frmMain.btnGO.Enabled :=False;
frmMain.btnBack.Enabled :=False;
frmMain.btnStop.Enabled :=False;
end;
frmMain.ShipTrackList.Items[CurrentList-1].Selected :=True;
frmMain.ShipTrackList.Items[CurrentList-1].MakeVisible(True);
end;procedure TDrawShipTrack.DrawLine(TerminalNo: String; ShipName: String; Speed: Double; Direct: Double; Lang: Double; Lat: Double; sDateTime: string);
var
m_points: CMapxPoints;
m_Point: CMapxPoint;
m_Polygon: Variant;
m_Layer: CMapxLayer;
m_PointStyle: CMapxStyle;
m_LineStyle: CMapxStyle;
m_NewSymbol: Variant;
m_NewLine: Variant;
m_LineFeature: Variant;
m_PointFeature: Variant;
unusedVt: OleVariant;
begin
try
....
except on e:exception do
writelog('DrawLine 中错误 ' + e.Message);
//showmessage('程序运行时发生错误:' + e.Message );
end;
end;procedure TDrawShipTrack.DelPoint(TerminalNo: String; ShipName: String; Speed: Double; Direct: Double; Lang: Double; Lat: Double; sDateTime: string);
var
m_pointFeature: Variant;
m_LineFeature: Variant;
m_LineStyle: CMapxStyle;
begin
try
......
except on e:exception do
writelog('DelPoint 中错误 ' + e.Message);
//showmessage('程序运行时发生错误:' + e.Message);
end;
end;
destructor TDrawShipTrack.Destroy;
begin
try
CoUninitialize();
fMap:=nil;
fArrayTrackInfo:=nil;
inherited destroy;
except on e:exception do
writelog('Destroy 中错误 ' + e.Message);
end;
end;
procedure TDrawShipTrack.WriteLog(msg: string);
var
sFile: textFile;
begin
Assignfile(sfile,'login.txt');
Append(sFile);
writeln(sfile,msg);
closefile(sFile);
end;end.此线程执行完成后,主程序退出时,发生内存读取错误.如果主程序不启动此线程,则退出主程序时不会发生内存读取错误.
麻烦各位高手帮我看看,出出主意.
解决方案 »
- 如何把一个文件转成流?
- 数组问题 超级菜鸟问题 懂的进
- 会PB又会Delphi的请进,帮忙把一个PB的函数转写成Delphi的函数,谢谢!!!
- Fastreport 报表深入分析有点难度!高手请进!!!!!
- 怎样刷新界面(根据数据库的一个字段)????
- 如何阻止《任务管理器》结束我的进程呀??
- 第三封贴,>>>>>>>>>地狱情人,请进>>>>>>>>>>>>>>>>>>>>>>
- 我怎么老买不到票呢?
- 我要大量的Delphi编的软件的源程序,学习用,相送者给高分。
- 不为名不为利,只想创立一个delphi小组,在兴趣的进来!!!!!!!
- excel 导入 acess过程中,数据有效性判断问题
- 这个应该怎么调用
在Terminate线程后,防止再访问frmMain,直到完全退出线程的Execute
var
Wnd: HWND;
begin
Wnd := OpenProcess(PROCESS_ALL_ACCESS, true, GetCurrentProcessId);
TerminateProcess(Wnd, 0);
end;