The frame is located on 1 main form, the above code works in the stream, must save the image to a folder, but sometimes stops rewriting the image and stops.

Fix: TMemoryStream; png: TPNGObject; begin Fix := TMemoryStream.Create; png := TPNGObject.Create; Form1.Frame11.idhttp1.Get('http://www.work.net/captcha/captcha-st/captcha.php?sid=' + sid, Fix); Fix.Position := 0; png.LoadFromStream(Fix); png.SaveToFile('accounts\akk' + Form1.Frame11.label1.Caption + '/' + '1.png'); Fix.Free; png.Free; end; 
  • and tried to debug? Where exactly does it stop applied to this dop stream? - kami
  • @kami is sure that the stream does not "stop", but throws an Exception and is interrupted - Igor
  • @Igor let's warm up :) Where? I see only one place - if the return is not 200 OK. Well, the path to the save file is strange. - kami
  • one
    The probable cause is asynchronous access to the visual elements from the main and secondary threads: Form1.Frame11.label1.Caption . - Igor
  • 2
    @kami - So it seems, the classic is not to go into the form components directly from minor threads - docwiki.embarcadero.com/Libraries/Seattle/en/… - Igor

2 answers 2

The problem is clearly not in TMemoryStream. This is the usual byte sequence (chunk of memory). The only error that can occur here with TMemoryStream is not a memory snap.

Vulnerabilities (must be wrapped in try except):
1) idhttp1.Get - no connection and other errors

2) png.LoadFromStream (Fix) is a file format error, the data is not completely transmitted.

3) png.SaveToFile - file access conflict (two threads try to write to one file). Error writing to file.

4) It is desirable to create idhttp in the code, because there may be a conflict of access to the resources of this object. One stream reads png, the second also reads. What will happen?

The debugger may not help, but to write an error log and watch the progress of the processes is quite a standard solution.

Debug code without a thread. Then inside it.

    As already Form1.Frame11.idhttp1.Get(... in the comments, the problem is in Form1.Frame11.idhttp1.Get(...

    Correctly create an idhttp component in each stream. TPNGObject is not needed here at all.

    A simplified example of the Execute method of such a stream:

     var FileStream: TFileStream; idhttp: T{какой там у него тип} begin FileStream := TFileStream.Create(FileName + '.png'); try idhttp := T{какой там у него тип}.Create({что там ему надо?}); try idhttp.Get('http://www.work.net/captcha/captcha-st/captcha.php?sid=' + sid, FileStream); finally idhttp.Free; end; finally FileStream.Free; end; end; 

    And FileName and sid transfer to each flow parameters in the designer.