Threads are being blocked by giving message in wait chain section "Blocked on critical section owned by thread xxxxx" if i give sleep after creating thread they are running fine. not sure why they are being blocked on critical section nothing much code in critical section. Can any one help to solve this issue.
My thread execute method which is having a global variable which is in critical section as shown below
procedure TMyThread.Execute();
Var
Filename : String;
FIleDone : Boolean;
begin
inherited;
FIleDone := False;
while not FIleDone do //while there are still files
begin
try
EnterCriticalSection(CriticalSection); //Try to catch the critical section
//Access the shared variables
//Are there still files available
if FileList.Count = 0 then
begin
//Leave the critical section, when there are no files left
LeaveCriticalSection(CriticalSection);
//Leave the while loop
FIleDone := true;
self.Terminate;
break;
end
else
begin
//Read the filename
Filename := FileList.Strings[0];
//Delete the file from the list
FileList.Delete(0);
//Leave the critical section
LeaveCriticalSection(CriticalSection);
CopyTable(ChangeFileExt(filename,''),Form1.TargetDir.Text);
end;
except
LeaveCriticalSection(CriticalSection);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15 : TMythread;
TimeThen: TDateTime;
TimeNow: TDateTime;
Counter,id1,id2 : Integer;
begin
TimeThen := now;
FileList := TStringList.create();
if Length(TargetDir.Text) > 1 then
if TargetDir.Text[Length(TargetDir.Text)] <> '\' then
TargetDir.Text := TargetDir.Text + '\';
GetFileStringList(TargetDir.Text + '*.db', FileList);
ProgressBar.Max := FileList.Count;
t1 := TMyThread.create(false);
//sleep(1000);
t2 := TMyThread.create(false);
//sleep(1000);
t3 := TMyThread.create(false);
//sleep(1000);
t4 := TMyThread.create(false);
//sleep(1000);
t5 := TMyThread.create(false);
//sleep(1000);
t6 := TMyThread.create(false);
//sleep(1000);
t7 := TMyThread.create(false);
//sleep(1000);
t8 := TMyThread.create(false);
//sleep(1000);
t9 := TMyThread.create(false);
//sleep(1000);
t10 := TMyThread.create(false);
//sleep(1000);
t11 := TMyThread.create(false);
//sleep(1000);
t12 := TMyThread.create(false);
//sleep(1000);
t13 := TMyThread.create(false);
//sleep(1000);
t14 := TMyThread.create(false);
//sleep(1000);
//t15 := TMyThread.create(false);
// sleep(1000);
//mythread.Execute;
while Done < 14 do
begin
progressBar.Position := ProgressBar.Max - FileList.Count;
Application.ProcessMessages;
end;
// end;
//ProgressBar.Position := ProgressBar.Position + 1;
//end;
//ChangeCOCompanyLegalName();
TimeNow := Now;
if ((TimeNow - TimeThen) * 24 * 60 * 60) < 60 then
ShowMessage('done in ' + FormatFloat('###',((Now - TimeThen) * 24 * 60 * 60)) + ' seconds')
else
if ((TimeNow - TimeThen) * 24 * 60) < 60 then
ShowMessage('done in ' + FormatFloat('###.00',((Now - TimeThen) * 24 * 60)) + ' minutes')
else
ShowMessage('done in ' + FormatFloat('###.00',((Now - TimeThen) * 24)) + ' hours');
//FileList.Free;
end;