顺序多线程

时间:2016-06-01 09:29:48

标签: multithreading delphi delphi-2010 tfilestream

我想为以下代码构建多线程过程,但无法正确构建它。我尝试使用Semaphore来顺序完成代码,但它只执行了{strong> else 部分下的myStream2.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;

0 个答案:

没有答案