F1:= TFileStream.Create('c:\t.zip',fmCreate or fmShareDenyNone);
F2:= TFileStream.Create('c:\t.zip',fmCreate or fmShareDenyNone);
第二个是就报不能建立文件,这么用不对吗?因为我要多线程并发写入一个文件,所以要对同一文件同时写入,还有别的方法吗?
F2:= TFileStream.Create('c:\t.zip',fmCreate or fmShareDenyNone);
第二个是就报不能建立文件,这么用不对吗?因为我要多线程并发写入一个文件,所以要对同一文件同时写入,还有别的方法吗?
互斥写: 用CreateMutex创建一个互斥变量
function WriteLog: boolean;
var
hMutex: HANDLE;
begin
hMutex:=CreateMutex(nil,false,'WriteLog');
if hMutex=nil then
return false;
if WAIT_OBJECT_0=WaitForSingleObject(hMutex,INFINITE) then
begin
//写日志...
return true;
end
else
return false;
end;
写完日志后,return前要ReleaseMutex(hMutex);