Solved

Convert Int64 to Int32 (Integer) ?

Posted on 2011-09-25
2
2,553 Views
Last Modified: 2016-09-29
Is it possible to convert int64 to in32 in delphi ?
0
Comment
Question by:rotem156
2 Comments
 
LVL 36

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 250 total points
ID: 36707476
calculate the percentage of the completion ... a lot easier for the progressbar
you only get numbers from 0 to 100
0
 
LVL 25

Accepted Solution

by:
epasquier earned 250 total points
ID: 36707917
first, use 1Gb parts, it will be a lot easier.

procedure Split1GoParts(TotalSize:Int64;Var NbFullParts,LastIncompletePartSize:Integer):
begin
 NbFullParts:=TotalSize SHR 30;
 LastIncompletePartSize:=TotalSize And $3FFFFFFF;
end;

Open in new window


then,in your thread Create, instead of using an EndOffset, use a Part Size to download (Integer).
You'll end up with a much cleaner thread code and an easier way to create those threads (only ONE int64 value is needed, the offset):
Somewhere in your code you'll have to use what Split1GoParts returns :

PartSize:=1 SHL 30;
for i:=0 to NbFullParts-1 do
 CreateThread ( alabel[I], apbar[I], hOpenFile[I], hInetFile[I], i*PartSize, PartSize);

Open in new window

Where CreateThread function is of course something that creates the thread and add it to your manager.
TFetchDataThread = class(TThread)
  private
    FStartOffset    : Int64;
    FPartSize       : Integer;
    FInetFile       : HINTERNET;
    FDestFile       : THandle;
    FProgressBar    : TProgressBar;
    FLabelProgress  : TLabel;
    FLoadedSize     : Integer;
    procedure ProgressGui;
  protected
    procedure Execute; override;
  public
    constructor Create(
    LabelProgress : TLabel;
    Progress      : TProgressBar;
    DestFile      : THandle;
    InetFile      : HINTERNET;
    StartOffset  : Int64;
    PartSize    : Integer);
    destructor Destroy;
  end;


constructor TFetchDataThread.Create(
LabelProgress: TLabel;
Progress     : TProgressBar;
DestFile     : THandle;
InetFile     : HINTERNET;
StartOffset  : Int64;
PartSize    : Integer);
begin
  FStartOffset    := StartOffset;
  FPartSize       := PartSize;
//  FEndOffset      := EndOffset;
  FInetFile       := InetFile;
  FDestFile       := DestFile;
  FProgressBar    := Progress;
  FLabelProgress  := LabelProgress;
  inherited Create( {DAPClass.UpdateProgressBar,} False );
end;

procedure TFetchDataThread.ProgressGui;
begin
  FProgressBar.Value := FLoadedSize;
  FProgressBar.Repaint; // Avoid gui problems
  FLabelProgress.Text :=
  Format( 'Thread: %d, Start Offset: %d (%x h), Loaded %d / %d (%f %%)',
   [ThreadID, FStartOffset, FStartOffset, FLoadedSize, FPartSize, 100*FLoadedSize/FPartSize ] );
end;

procedure TFetchDataThread.Execute;
type
  TypeByteArray = array [1..1024*2] of Byte;
var
  Buffer            : TypeByteArray;
  BytesToReadWrite,
  BufferLen,
  BytesWritten,
begin
 FreeOnTerminate := True;
 FProgressBar.Min :=  0;
 FProgressBar.Max :=  FPartSize;
 InternetSetFilePointer( FInetFile, FStartOffset, nil, FILE_BEGIN, 0 );
 SetFilePointer( FDestFile, FStartOffset, nil, FILE_BEGIN );
 try
  repeat
   BytesToReadWrite := FPartSize - FLoadedSize;
   if BytesToReadWrite > SizeOf( Buffer ) 
     Then BytesToReadWrite  := SizeOf( Buffer )

   InternetReadFile( FInetFile, @Buffer, BytesToReadWrite, BufferLen );

   LockFile( FDestFile, FStartOffset, 0, BytesToReadWrite, 0 );
   WriteFile( FDestFile, Buffer, BytesToReadWrite, BytesWritten, nil );
   UnlockFile( FDestFile, FStartOffset, 0, BytesToReadWrite, 0 );
   Inc(FLoadedSize,BytesWritten);
   
   Synchronize( ProgressGui );

// Why reducing CPU in the download thread ? it will just slow down everything.
//    Sleep( Random( 15 ) ); // Avoid cpu usage
  until (FLoadedSize>=FPartSize) or (Terminated);
 finally
  CloseHandle( FDestFile );
  InternetCloseHandle( FInetFile );
 end;
end;

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now