Multi-thread download (idhttp)

Source: Internet
Author: User

The younger brother wrote a multi-thread download Program There are always signs of an endless loop, but I think it may be a deadlock. However, an error occurs during one-step tracking:
Unit indymthread;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls, comctrls, idbasecomponent, idcomponent,
Idtcpconnection, idtcpclient, idhttp;

Type
Tform1 = Class (tform)
Btndowload: tbutton;
Btncancel: tbutton;
Edturl: tedit;
Label1: tlabel;
Redtmess: trichedit;
Idhttp1: tidhttp;
Procedure btndowloadclick (Sender: tobject );
Procedure formcreate (Sender: tobject );
Procedure formdestroy (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
Tthreadload = Class (tthread)
Private
{Private declatation}
M_surl: string;
Protected
{Protected declaration}
Procedure GetFile (VAR sfilename: string );
Procedure reditadd;
Procedure execute; override;
Public
{Public declarations}
RedT: trichedit;
Constructor create (bwait: Boolean; const URL: string );

// Destructor destroy; override;
End;
VaR
Form1: tform1;
M_arrayimage: array [0 .. 4] of string;
G_ I: integer;
Criticalsec: trtlcriticalsection;
Implementation

{$ R *. DFM}
Procedure tthreadload. GetFile (VAR sfilename: string );
VaR
I: integer;
Begin
For I: = 0 to 4 do
Begin
If m_arrayimage [I] = ''then continue;
Sfilename: = m_arrayimage [I];
M_arrayimage [I]: = '';
Exit;
End;
End;

Procedure tthreadload. reditadd;
Begin
RedT. lines. append ('saving ');
End;
Procedure tthreadload. Execute;
VaR

Idhttp2: tidhttp;
File_name: string;
Streamfile1: tmemorystream;
Begin

While not terminated do
Begin
Entercriticalsection (criticalsec); // enter the critical section
Try
GetFile (file_name );
Idhttp2: = tidhttp. Create (NiL );
Try
Streamfile1: = tmemorystream. Create;
Try
Idhttp2.head (m_surl );
Idhttp2.get (m_surl, streamfile1 );
Streamfile1.savetofile ('d: \ '+ file_name );
Finally
Streamfile1.free;
End;
Finally
Idhttp2.free;
End;
// Synchronize (reditadd );
Finally
Leavecriticalsection (criticalsec );
End;
If file_name = ''then exit;
End; // begin

End;

Procedure tform1.btndowloadclick (Sender: tobject );
VaR
I, K: integer;
Threadarray: array [0 .. 4] of tthreadload;
Begin
For I: = 0 to 4 do
Begin
Threadarray [I]: = tthreadload. Create (true, edturl. Text + '/' + m_arrayimage [I]);
Threadarray [I]. RedT: = redtmess;
End;
For K: = 0 to 4 Do threadarray [K]. Resume;
End;

Constructor tthreadload. Create (bwait: Boolean; const URL: string );
Begin
Inherited create (bwait );
M_surl: = URL;
End;

{Destructor tthreadload. Destroy;
Begin
Inherited destroy;
End ;}

Procedure tform1.formcreate (Sender: tobject );
Begin
M_arrayimage [0]: parameter 'aa.bmp ';
M_arrayimage [4]: Invalid '4.jpg ';
M_arrayimage [2]: '1.jpg ';
M_arrayimage [3]: Invalid '2.jpg ';
M_arrayimage [1]: '3.jpg ';
G_ I: = 0;
Initializecriticalsection (criticalsec)
End;

Procedure tform1.formdestroy (Sender: tobject );
Begin
Deletecriticalsection (criticalsec );
End;

End.
{===================================================== =================================}
An error message is generated during Delphi's single-step tracking. Sometimes, the entire Delphi will die.
Drop


{===================================================== =================================}

Continue to read the full text of "multi-thread download problem (idhttp...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.