只有一个线程,不存在线程同步啊。代码如下://创建VCL代码 function TfrmMain.LoadMonitor: Boolean; var tblMonitor: TADOTable; i: Integer; MonitorInfo: TMonitor; begin Result := False; //打开数据表 tblMonitor := dm.GetMonitorTable(); if(not tblMonitor.Active) then Exit; //创建下位机管理对象 MonitorManager := TMonitorManager.Create(g_nCommPort, g_nBaud, g_nDataBit, g_nStopBit, g_nParity, tblMonitor.RecordCount); SetLength(BwViewList, tblMonitor.RecordCount); // if(not MonitorManager.IsCommOpened) then // begin // Result := False; // Exit; // end; tblMonitor.First; for i := 0 to tblMonitor.RecordCount-1 do begin MonitorInfo.MonitorNo := tblMonitor.FieldByName('MonitorNo').AsInteger; MonitorInfo.MonitorName := tblMonitor.FieldByName('MonitorName').AsString; MonitorInfo.AddrNo := tblMonitor.FieldByName('AddrNo').AsInteger; MonitorInfo.IsAutoWork := tblMonitor.FieldByName('IsAutoWork').AsBoolean; MonitorInfo.TemMaxValue := tblMonitor.FieldByName('TemMaxValue').AsInteger; MonitorInfo.TemMinValue := tblMonitor.FieldByName('TemMinValue').AsInteger; MonitorInfo.HumMaxValue := tblMonitor.FieldByName('HumMaxValue').AsInteger; MonitorInfo.HumMinValue := tblMonitor.FieldByName('HumMinValue').AsInteger; MonitorManager.SetMonitor(i, MonitorInfo); bwv := TBohiWatchView.Create(nil); bwv.Width := BwvWidth; bwv.Height := BwvHeight; bwv.Parent := sbView; bwv.BWV_ApparatusNo := MonitorInfo.MonitorNo; bwv.BWV_ApparatusTitle := MonitorInfo.MonitorName; bwv.OnTitleClick := OnBwViewTitleClick; bwv.Visible := False; BwViewList[i] := bwv;
tblMonitor.Next; end; Result := True; end;//线程代码 procedure TMonitorThread.Execute; var i: Integer; begin inherited;
while True do begin //等待停止事件 if(WAIT_OBJECT_0 = WaitForSingleObject(thrHandleStop, 0)) then begin SetEvent(thrHandleStopped); Break; end; //开始轮巡下位机 for i := 0 to frmMain.MonitorManager.GetCount() do begin nIndex := i; nTemValue := Random(100); nHumValue := Random(100); try // Synchronize(UpdateMonitor); except end; end; Sleep(Interval); end; end;
呵呵,搂住创建了新的线程又使用主线程的元素,我想还是需要同步的,当然从搂主的代码来看关键部分已经同步,唯一可能同步出错的应该是GetCount的返回值了. 你的以下代码不会越界吗? for i := 0 to frmMain.MonitorManager.GetCount() do 是不是应该: for i := 0 to frmMain.MonitorManager.GetCount()-1 do
////
这里指什么?
function TfrmMain.LoadMonitor: Boolean;
var
tblMonitor: TADOTable;
i: Integer;
MonitorInfo: TMonitor;
begin
Result := False; //打开数据表
tblMonitor := dm.GetMonitorTable();
if(not tblMonitor.Active) then Exit; //创建下位机管理对象
MonitorManager := TMonitorManager.Create(g_nCommPort, g_nBaud, g_nDataBit,
g_nStopBit, g_nParity, tblMonitor.RecordCount); SetLength(BwViewList, tblMonitor.RecordCount);
// if(not MonitorManager.IsCommOpened) then
// begin
// Result := False;
// Exit;
// end; tblMonitor.First; for i := 0 to tblMonitor.RecordCount-1 do
begin
MonitorInfo.MonitorNo := tblMonitor.FieldByName('MonitorNo').AsInteger;
MonitorInfo.MonitorName := tblMonitor.FieldByName('MonitorName').AsString;
MonitorInfo.AddrNo := tblMonitor.FieldByName('AddrNo').AsInteger;
MonitorInfo.IsAutoWork := tblMonitor.FieldByName('IsAutoWork').AsBoolean;
MonitorInfo.TemMaxValue := tblMonitor.FieldByName('TemMaxValue').AsInteger;
MonitorInfo.TemMinValue := tblMonitor.FieldByName('TemMinValue').AsInteger;
MonitorInfo.HumMaxValue := tblMonitor.FieldByName('HumMaxValue').AsInteger;
MonitorInfo.HumMinValue := tblMonitor.FieldByName('HumMinValue').AsInteger; MonitorManager.SetMonitor(i, MonitorInfo); bwv := TBohiWatchView.Create(nil); bwv.Width := BwvWidth;
bwv.Height := BwvHeight;
bwv.Parent := sbView;
bwv.BWV_ApparatusNo := MonitorInfo.MonitorNo;
bwv.BWV_ApparatusTitle := MonitorInfo.MonitorName;
bwv.OnTitleClick := OnBwViewTitleClick;
bwv.Visible := False; BwViewList[i] := bwv;
tblMonitor.Next;
end; Result := True;
end;//线程代码
procedure TMonitorThread.Execute;
var
i: Integer;
begin
inherited;
while True do
begin
//等待停止事件
if(WAIT_OBJECT_0 = WaitForSingleObject(thrHandleStop, 0)) then
begin
SetEvent(thrHandleStopped);
Break;
end; //开始轮巡下位机
for i := 0 to frmMain.MonitorManager.GetCount() do
begin
nIndex := i;
nTemValue := Random(100);
nHumValue := Random(100); try
// Synchronize(UpdateMonitor);
except
end;
end; Sleep(Interval);
end;
end;
procedure TMonitorThread.UpdateMonitor();
begin
frmMain.BwViewList[nIndex].BWV_TemValue := nTemValue;
frmMain.BwViewList[nIndex].BWV_HumValue := nHumValue;
end;
你的以下代码不会越界吗?
for i := 0 to frmMain.MonitorManager.GetCount() do
是不是应该:
for i := 0 to frmMain.MonitorManager.GetCount()-1 do
55555555555555555结了。