메인화면에서 조회버튼을 클릭하면
DAEJEON_CITY_THREAD; 쓰레드를 호출하고
procedure는 다른 pas에 존재한다.
호출된 procedure 내용을 다음과 같다
procedure DAEJEON_CITY_THREAD;
var
FDAEJEON_CITY_THREAD : TDAEJEON_CITY_THREAD;
begin
FDAEJEON_CITY_THREAD := TDAEJEON_CITY_THREAD.Create(True);
FDAEJEON_CITY_THREAD.Priority := tpNormal;
try
FDAEJEON_CITY_THREAD.Resume;
Sleep(1);
except
on E: Exception do
begin
FDAEJEON_CITY_THREAD.Free;
ShowMessage('오류내용:'+E.Message);
end;
end;
end;
//===========================쓰레드 내용
unit NIS_DAEJEON_CITY_THREAD;
interface
uses
Classes, DB, MemDS, DBAccess, Ora, SysUtils, QDialogs, IniFiles, QForms;
type
TDAEJEON_CITY_THREAD = class(TThread)
private
{ Private declarations }
SVRLIST : TIniFile;
DaeJeon_City_Ora_Ses : TOraSession;
DaeJeon_City_qry : TOraQuery;
protected
procedure Execute; override;
procedure ThreadsDone(Sender: TObject);
end;
implementation
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TDaeJeonThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TDAEJEON_CITY_THREAD }
uses NIS_STATE;
var
DaeJeon_City_State : Array [0..4] of String;
procedure TDAEJEON_CITY_THREAD.Execute;
var
Ls_Username : String;
Ls_Password : String;
Ls_Server : String;
begin
{ Place thread code here }
FreeOnTerminate := True;
OnTerminate := ThreadsDone;
SVRLIST := TIniFile.Create(ExtractFilePath(Application.ExeName)+'대전.INI');
Ls_Username := SVRLIST.ReadString('SERVER1', 'USERNAME', '');
Ls_Password := SVRLIST.ReadString('SERVER1', 'PASSWORD', '');
Ls_Server := SVRLIST.ReadString('SERVER1', 'SERVER', '');
SVRLIST.Free;
DaeJeon_City_Ora_Ses := TOraSession.Create(DaeJeon_City_Ora_Ses);
DaeJeon_City_Ora_Ses.Name := 'DaeJeon_City_Ora_Sess';
DaeJeon_City_Ora_Ses.ConnectPrompt := False;
DaeJeon_City_Ora_Ses.Username := Ls_Username;
DaeJeon_City_Ora_Ses.Password := Ls_Password;
DaeJeon_City_Ora_Ses.Server := Ls_Server;
DaeJeon_City_Ora_Ses.Options.Net := True;
DaeJeon_City_Ora_Ses.Options.EnableIntegers := True;
DaeJeon_City_Ora_Ses.Options.NeverConnect := False;
DaeJeon_City_Ora_Ses.Options.UseOCI7 := False;
DaeJeon_City_Ora_Ses.ThreadSafety := True;
try
DaeJeon_City_Ora_Ses.Connect;
except
// 연결되지 않으므로 오류 내용을 찍는다
// 해당서버 단체명을 뿌려준다
DaeJeon_City_State[00] := '대전 본청 - 자료없슴';
DaeJeon_City_State[01] := '0';
DaeJeon_City_State[02] := '0';
DaeJeon_City_State[03] := '0';
DaeJeon_City_State[04] := '0';
end;
end;
procedure TDAEJEON_CITY_THREAD.ThreadsDone(Sender: TObject);
function GF_Chrcut(GPs_v:string ; GPs_xch:string): string;
begin
while pos(GPs_xch,GPs_V) > 0 do
begin
delete(GPs_V,pos(GPs_xch,GPs_v),1);
end;
if GPs_V = '' then Result := '0'
else Result := GPs_V;
end;
function GF_Chrcut_I(GPs_v:string ; GPs_xch:string): Integer;
begin
while pos(GPs_xch,GPs_V) > 0 do
begin
delete(GPs_V,pos(GPs_xch,GPs_v),1);
end;
if GPs_V = '' then Result := 0
else Result := StrToInt(GPs_V);
end;
var
j : Integer;
RowNo : Integer;
Svr_Row : String;
CityName : String;
begin
DaeJeon_City_qry := TOraQuery.Create(DaeJeon_City_qry);
DaeJeon_City_qry.Session := DaeJeon_City_Ora_Ses;
with DaeJeon_City_qry do
begin
Close;
Sql.Clear;
Sql.Text := ' SELECT COUNT(1) CNT, ''부'' GUBUN '
+ ' FROM RISTNLVBOOK '
+ ' WHERE LVY_STT_GBN = ''01'' ' //부과
+ ' AND LVY_YMD <= '+QuotedStr(FormatDateTime('YYYYMMDD', wf_State.edt_Acc_Year.Date))
+ ' UNION '
+ ' SELECT COUNT(1) CNT, ''체'' GUBUN '
+ ' FROM RISTNLVBOOK '
+ ' WHERE LVY_STT_GBN = ''02'' ' //체납
+ ' AND LVY_YMD <= '+QuotedStr(FormatDateTime('YYYYMMDD', wf_State.edt_Acc_Year.Date));
Open;
end;
for j := 0 to DaeJeon_City_qry.RecordCount-1 do
begin
if DaeJeon_City_qry.FieldByName('GUBUN').AsString = '부' then
begin
DaeJeon_City_State[03] := FormatFloat('#,##0',DaeJeon_City_qry.FieldByName('CNT').AsFloat);
DaeJeon_City_State[02] := FormatFloat('#,##0',StrToFloat(GF_Chrcut(DaeJeon_City_State[02],','))+
StrToFloat(GF_Chrcut(DaeJeon_City_State[03],',')));
end
else
if DaeJeon_City_qry.FieldByName('GUBUN').AsString = '체' then
begin
DaeJeon_City_State[04] := FormatFloat('#,##0',DaeJeon_City_qry.FieldByName('CNT').AsFloat);
DaeJeon_City_State[02] := FormatFloat('#,##0',StrToFloat(GF_Chrcut(DaeJeon_City_State[02],','))+
StrToFloat(GF_Chrcut(DaeJeon_City_State[04],',')));
end;
DaeJeon_City_qry.Next;
end;
RowNo := 0;
if wf_State.Chk_Gbn then
begin // 전체를 선택했을경우
Svr_Row := '';
SVRLIST := TIniFile.Create(ExtractFilePath(Application.ExeName)+'대전.INI');
Svr_Row := SVRLIST.ReadString('SERVER_CODE', 'SERVER1', '');
SVRLIST.Free;
SVRLIST := TIniFile.Create(ExtractFilePath(Application.ExeName)+'SVRLIST.INI');
RowNo := SVRLIST.ReadInteger('SERVER_ROW', Svr_Row, 0);
SVRLIST.Free;
end
else
begin
SVRLIST := TIniFile.Create(ExtractFilePath(Application.ExeName)+'대전.INI');
RowNo := SVRLIST.ReadInteger('SEVER_NO', 'SERVER1', 0);
SVRLIST.Free;
end;
SVRLIST := TIniFile.Create(ExtractFilePath(Application.ExeName)+'대전.INI');
CityName := SVRLIST.ReadString('SERVER_LIST', 'SERVER1', '');
SVRLIST.Free;
// 실과수
DaeJeon_City_qry.FetchAll := True;
with DaeJeon_City_qry do
begin
Close;
Sql.Clear;
Sql.Text := 'SELECT DISTINCT DEP_CODE '
+ ' FROM RISTNLVBOOK ';
Open;
end;
DaeJeon_City_State[01] := FormatFloat('#,##0',DaeJeon_City_qry.RecordCount);
wf_State.gdrd_Main.Cells[00, RowNo] := CityName;
wf_State.gdrd_Main.Cells[01, RowNo] := DaeJeon_City_State[01];
wf_State.gdrd_Main.Cells[02, RowNo] := DaeJeon_City_State[02];
wf_State.gdrd_Main.Cells[03, RowNo] := DaeJeon_City_State[03];
wf_State.gdrd_Main.Cells[04, RowNo] := DaeJeon_City_State[04];
wf_State.gdrd_Main.Cells[01, 1] := FormatFloat('#,##0',GF_Chrcut_I(DaeJeon_City_State[01],',')+GF_Chrcut_I(wf_State.gdrd_Main.Cells[01, 1],','));
wf_State.gdrd_Main.Cells[02, 1] := FormatFloat('#,##0',GF_Chrcut_I(DaeJeon_City_State[02],',')+GF_Chrcut_I(wf_State.gdrd_Main.Cells[02, 1],','));
wf_State.gdrd_Main.Cells[03, 1] := FormatFloat('#,##0',GF_Chrcut_I(DaeJeon_City_State[03],',')+GF_Chrcut_I(wf_State.gdrd_Main.Cells[03, 1],','));
wf_State.gdrd_Main.Cells[04, 1] := FormatFloat('#,##0',GF_Chrcut_I(DaeJeon_City_State[04],',')+GF_Chrcut_I(wf_State.gdrd_Main.Cells[04, 1],','));
wf_State.gdrd_Main.Refresh;
DaeJeon_City_Ora_Ses.Free;
DaeJeon_City_qry.Free;
end;
end.
이런한 내용의 pas를 지역별로 여러개 생성해서
메인에서 조회를 클릭하면 여러 지역 쓰레드를
계속적으로 호출한다
어쩔때는 정상적으로 되다가 어쩔때는
다음과 같은 에러가 나오는데 에러 내용을 다음과 같다.
"OCI_INVALID_HANDLE" 이것이 한번 나오기 시작하면
계속해서 문제가 생겨서 실행되지 않는다
즐거운 하루 되세요
Excute에 직접 Coding은 되도록 자제하시구여
특정 Procedure나 Function을 작성하시구..
그곳에서 기능을 모두 Coding하시구여
Synchronize 을 이용해서 해당 Procedure를 호출해 주세요..
그렇게 하지 않는다면,
어떤 Thread가 다른 Thread에 대한 참조를 할때
Class문제가 발생할 소지가 있어
님의 에러가 발생할수가 있습니다..
Delphi는 Class가 있지만, 똑같은 Class는
Title로서 구분한다는거 아시져?
그런 비슷한원리로 인해서.. Handle에러가 발생할 수 있습니다.
뭐.. 간단한 예를 들어서 그렇지만..
깊이들어가면, 다른 요소도 있겠져..
어쨌든..
Synchronize를 이용하시면, Error가 발생치 않을겁니다..
그럼..