Solved

Convert Int64 to Int32 (Integer) ?

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…

822 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