0

我想为以下代码构建多线程过程,但无法正确构建它。我尝试使用 Semaphore 顺序完成代码,但它只执行elsemyStream2.CopyFrom(myStream1, StrToInt64('$' + SL1[(i - 1)]));部分下的部分。出现的问题是,如果我们随意将这些流恢复到输出文件流 (mystream2) 中,文件就会损坏。

谁能帮助我。

  for i := 1 to SL1.Count do
  begin
    if Length(SL1[i - 1]) > 12 then
    begin
      TempDir := SrcDir + 'temp_' +
              IntToHex(Random(2147483647) + Random(2147483647), 8) + '\';
      ForceDirectories(TempDir);
      for k := 1 to 2 do
      begin
        tempfile := TempDir + IntToHex((i - 1), 8) + '.tmp' + IntToStr(k);
        fs := TFileStream.Create(tempfile, fmCreate or fmShareExclusive);
        fs.CopyFrom(myStream1, StrToInt64('$' + DecodeRefStr(SL1[(i - 1)])[k]));
        fs.Free;
      end;
      ExecAndWait(SrcDir + 'process.exe', Method + ' ' + '"' + TempDir +
                  IntToHex((i - 1), 8) + '.tmp1' + '" "' + TempDir +
                  IntToHex((i - 1), 8) + '.tmp2' + '" "' + TempDir +
                  IntToHex((i - 1), 8) + '.tmp4' + '"', TempDir, 
                  True,  True);
      tempfile := TempDir + IntToHex((i - 1), 8) + '.tmp4';
      fs := TFileStream.Create(tempfile, fmOpenRead);
      myStream2.CopyFrom(fs, 0);
      fs.Free;
      TDirectory.Delete(TempDir, True);
    end
    else
      myStream2.CopyFrom(myStream1, StrToInt64('$' + SL1[(i - 1)]));
    end;
    myStream1.Free;
    myStream2.Free;
4

0 回答 0