1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
Unit main;
Interface
Uses
{$IFDEF LINUX}
libc, QForms, QStdCtrls, QControls, QGraphics, QDialogs, QExtCtrls,
{$ELSE}
Windows, Graphics, Controls, Forms, Messages, Dialogs, StdCtrls,
{$ENDIF}
SysUtils, Classes, uib, SyncObjs, JvTimer, JvExStdCtrls, JvMemo, JvEdit, JvValidateEdit, JmEdit; // , JvTimer, JvExStdCtrls, JvMemo; // , JvExStdCtrls, JvMemo, JvTimer;
{$DEFINE WITH_THREAD}
Type
TForm1 = Class(TForm)
DataBase: TUIBDataBase;
Button1: TButton;
JvMemo1: TJvMemo;
JvTimer1: TJvTimer;
JmEdit1: TJmEdit;
Procedure Button1Click(Sender: TObject);
Procedure JvTimer1Timer(Sender: TObject);
Private
{ Déclarations privées }
Public
{ Déclarations publiques }
End;
TMyThread = Class(TThread)
Protected
TheEnd: Boolean;
{$IFDEF WITH_THREAD}
Procedure Execute; Override;
{$ENDIF}
Destructor destroy; Override;
End;
Var
Form1 : TForm1;
Err : String = '';
Info : String = '';
// MutexUib : THandle;
CriticalSection: TRTLCriticalSection;
Implementation
{$R *.dfm}
Var
NuTh: integer = 0;
{ TMyThread }
Destructor TMyThread.destroy;
Begin
Inherited;
End;
{$IFDEF WITH_THREAD}
Procedure TMyThread.Execute;
{$ELSE}
Procedure Execute;
{$ENDIF}
Var
Query : TUIBQuery;
Transaction : TUIBTransaction;
S1, Etape : String;
NuLect, MyNuTh: integer;
Begin
{$IFDEF WITH_THREAD}
FreeOnTerminate := True;
{$ENDIF}
TheEnd := False;
inc(NuTh);
MyNuTh := NuTh;
// Form1.DataBase.Lock; //simulate single thread
Try
Etape := 'Create Transac & Query';
NuLect := 0;
Query := TUIBQuery.Create(Nil);
Transaction := TUIBTransaction.Create(Nil);
Try
Transaction.DataBase := Form1.DataBase;
Query.Transaction := Transaction;
Query.FetchBlobs := True;
Query.SQL.Text := 'select * from poste';
// Query.SQL.Text := 'select * from project';
// WaitForSingleObject(MutexUib, INFINITE);
EnterCriticalSection(CriticalSection);
Etape := 'Query.open';
Err := '** Thread=' + IntToStr(MyNuTh) + ' NuLect=' + IntToStr(NuLect) + ' Etape=' + Etape;
Query.Open;
sleep(10); // simulate activity
// ReleaseMutex(MutexUib);
LeaveCriticalSection(CriticalSection);
While Not Query.EOF Do
Begin
inc(NuLect);
Etape := 'Query.Fields.ByNameAsString';
S1 := Query.Fields.ByNameAsString['cod_poste'];
// WaitForSingleObject(MutexUib, INFINITE);
EnterCriticalSection(CriticalSection);
Etape := 'Query.Next';
Query.Next;
// ReleaseMutex(MutexUib);
// Err := '** Thread=' + IntToStr(MyNuTh) + ' NuLect=' + IntToStr(NuLect) + ' Etape=' + Etape;
LeaveCriticalSection(CriticalSection);
sleep(1); // simulate activity
End;
Except
On Erreur: Exception Do
Begin
Err := '** Thread=' + IntToStr(MyNuTh) + ' NuLect=' + IntToStr(NuLect) + ' Etape=' + Etape + ' Except=' + Erreur.Message;
End;
End;
Finally
EnterCriticalSection(CriticalSection);
// WaitForSingleObject(MutexUib, INFINITE);
Etape := 'Query.Close';
Err := '** Thread=' + IntToStr(MyNuTh) + ' NuLect=' + IntToStr(NuLect) + ' Etape=' + Etape;
// Form1.JvMemo1.Lines.Add(Info);
Query.Close(etmCommit);
sleep(10); // simulate activity
// ReleaseMutex(MutexUib);
LeaveCriticalSection(CriticalSection);
Query.Free;
Transaction.Free;
// Form1.DataBase.UnLock; //simulate single thread
End;
TheEnd := True;
End;
Procedure TForm1.JvTimer1Timer(Sender: TObject);
Begin
JvTimer1.Enabled := False;
If Err <> '' Then
Begin
JvMemo1.Lines.Add('**** ' + Err);
Err := '';
End;
// If Info <> '' Then
// Begin
// JvMemo1.Lines.Add(Info);
// Info := '';
// End;
JvTimer1.Enabled := True;
End;
Procedure TForm1.Button1Click(Sender: TObject);
Var
i : integer;
MonThread: TMyThread;
TickStart: Int64;
Begin
TickStart := GetTickcount;
For i := 0 To 49 Do
// For i := 0 To 9 Do
Begin
JvMemo1.Lines.Add('=========> Lancement ' + IntToStr(i));
{$IFDEF WITH_THREAD}
MonThread := TMyThread.Create(False);
MonThread.TheEnd := False;
// While Not(MonThread.TheEnd) Do
// Begin
// Application.ProcessMessages;
// End;
{$ELSE}
Execute;
{$ENDIF}
End;
JvMemo1.Lines.Add('=========> FIN');
End;
Initialization
//MutexUib := CreateMutex(Nil, False, 'MutexUib');
InitializeCriticalSection(CriticalSection);
End. |
Partager