多线程用 Writeln时有时会出错,请指教(50分)

  • 主题发起人 主题发起人 study2003
  • 开始时间 开始时间
S

study2003

Unregistered / Unconfirmed
GUEST, unregistred user!
在线程里面有如下的语句,
Writeln(F,'1'+'@@@'+StudentName+'@@@');
各个线程有各自的F,不会冲突的。
但是如下面所示,
1@@@王巍俊@@@
1@@@张啸霏@@@
1@@@y
第三个(1@@@y)还没完整就结束了,线程也自动消失了,没释放。
请指教,谢谢
 
线程之间没有干扰,那问题肯定是对同一个文件操作了
 
不会啊,我检测到每条线程各自打开的文件都不一样的。
他们不会同时打开同一个文件的
 
要么把代码写全点,要么自己解决 [:D]
 
大致代码如下
procedure GetHttpFileThread.Execute;
var
temp,i,j,k,ALlPage:integer;
// idhttp1:TIdHTTP;
pageSong,PageDl,PageGeci,TempContent:AnsiString;
SingerID:integer;
SingerName:string;
SongDLAddress,TempSongDLAddress,geciDLAddress,SongName,DlName:string;
SongId:String;
F_TempSongName,F_TempGeCi,F_TempDL,TempSongName,TempGeCi,TempDL:String;
SongNameFile,GeCiFile,DlFile:TextFile;
TempPageSong:AnsiString;
begin
try
// Idhttp1:=Tidhttp.Create(nil);
begin
while (not Terminated)do
begin
EnterCriticalSection(CS);
if counter <total then
begin
counter:=counter+1;
temp:=Counter;
LeaveCriticalSection(CS);
end
else
begin
LeaveCriticalSection(CS);
exit;
end;
SingerName:=Form1.StringGrid4.Cells [1,temp];
SingerLianjie:=Form1.StringGrid4.Cells [2,temp];
SingerId:=StrToInt(Form1.StringGrid4.Cells [3,temp]);
lianjie:=SingerName+SingerLianjie+SingerId;
try
F_TempSongName:='X'+SIngerName+','+IntToStr(singerid)+','+'.txt';
TempSongName:=SingerName+','+IntToStr(singerid)+','+'.txt';
AssignFile(SongNameFile,F_TempSongName);
Rewrite(SongNameFile);
except
end;
temp_Data := TstringStream.Create('');
try
Now_lianjie:=lianjie;
httpcli1.URL:=Lianjie;
httpcli1.RcvdStream := temp_Data;
form1.CSObject1.SendMsg(csmWarning,inttostr(index)+','+Now_Lianjie);
httpcli1.Get ;
page:='';
page:=temp_data.DataString;
except
continue;
end;
Analyze(page);
end;
end;

Procedure GetHttpFileThread.Analyze(text:AnsiString);
var
i,k,TempNowStudentNumber,TempMostStudentNumber:integer;
studentname,StudentMail,StudentChinarenName:string;
begin

k:=pos('class=cr2lk>',line);
if k<>0 then
delete(line,1,k+11);
while (k<>0)do
begin
StudentName:=copy(line,1,pos('</a>',line) - 1);
k:=pos('class=cr2lk>',line);
delete(line,1,k+11);
StudentChinarenName:=copy(line,1,pos('</a>',line) - 1);
k:=pos('class=cr2>',line);
delete(line,1,k+9);
StudentMail:=copy(line,1,pos('</a>',line) - 1);
k:=pos('class=cr2lk>',line);
if k<>0 then
delete(line,1,k+11);
if goonid='' then
begin
// sleep(100);
Writeln(F,'1'+'@@@'+StudentName+'@@@');
end
else
begin
Append(f);
Writeln(F,'1'+'@@@'+StudentNameid);
Flush(f);
end;

end;

end;

end.
 
后退
顶部