Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3800374
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:52:57+00:00 2026-05-19T13:52:57+00:00

I’ve got a almost completed app now and the next feature I want to

  • 0

I’ve got a almost completed app now and the next feature I want to implement is threading. I chose to go with BeginThread(), although am aware of TThread in delphi. The problem I’m coming across is the structure of BeginThread() call. Normally the line in the program that would call the function I want to be threaded is

CompareFiles(form1.Edit3.Text,Form1.Edit4.Text,Form1.StringGrid2,op);

op is a integer.

The line I’ve switched it out for to create a thread from it is

BeginThread(nil,0,CompareFiles,Addr('form1.Edit3.Text,Form1.Edit4.Text,Form1.StringGrid2,op'),0,x);

From the little amount of infromation I can find on how to actually use BeginThread() this should be a fine call, however on compling all I get is complier errors regarding the structure of my BeginThread() statement paramenters.

EDIT FOR INFORMATION.

The current procedure that calls CompareFiles is

procedure TForm1.Panel29Click(Sender: TObject);
var
op,x : integer;

begin
    if (Form1.Edit3.Text <> '') AND (Form1.Edit4.Text <> '') then
        begin
          op := 3;
          if RadioButton7.Checked = True then op := 0;
          if RadioButton3.Checked = True then op := 1;
          if RadioButton4.Checked = True then op := 2;
          if RadioButton5.Checked = True then op := 3;
          if RadioButton6.Checked = True then op := 4;
          CompareFiles(form1.Edit3.Text,Form1.Edit4.Text,Form1.StringGrid2,op);
        end;
end;

If I was to use TThread as suggested by a couple of people, and as displayed by Rob below, I’m confused at how a) I would pass op,Edit3/4.Text and StringGrid2 to the CompareFiles. Guessing from the example of TThread I’ve seen I thought I would replace the code above with TCompareFilesThread.Executeand the put the current code from Panel29Click into TCompareFilesThread.Create and then add

FEdit3Text := Edit3Text;
FEdit4Text := Edit4Text;
FGrid := Grid;

to this

FEdit3Text := Form1.Edit3.Text;
FEdit4Text := Form1.Edit4.Text;
FGrid := Form1.StringGrid2;

But I’ve got this nagging feeling that is totally off the mark.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:52:58+00:00Added an answer on May 19, 2026 at 1:52 pm

    That’s not at all the way to use BeginThread. That function expects a pointer to a function that takes one parameter, but the function you’re trying to call wants four. The one parameter you’re giving to BeginThread for it to forward to the thread procedure is a string, but you evidently hope that some sort of magic will turn that string of characters into the values that those variables contain.

    That’s not how Delphi works, and even for the languages that can do something like that, it’s generally discouraged to actually do it.

    To pass multiple parameters to BeginThread, define a record with all the values you’ll need, and also define a record pointer:

    type
      PCompareFilesParams = ^TCompareFilesParams;
      TCompareFilesParams = record
        Edit3Text,
        Edit4Text: string;
        Grid: TStringGrid;
        Op: Integer;
      end;
    

    Change CompareFiles to accept a pointer to that record:

    function CompareFiles(Params: PCompareFilesParams): Integer;
    

    To start the thread, you’ll need to allocate an instance of that record and populate its fields:

    var
      Params: PCompareFilesParams;
    begin
      New(Params);
      Params.Edit3Text := Edit3.Text;
      Params.Edit4Text := Edit4.Text;
      Params.Grid := StringGrid2;
      Params.Op := op;
      BeginThread(nil, 0, @CompareFiles, Params, 0, x);
    

    Implement CompareFiles like this so that the record will get freed before the thread terminates:

    function CompareFiles(Params: PCompareFilesParams): Integer;
    begin
      try
        // <Normal implementation goes here.>
      finally
        Dispose(Params);
      end;
    end;
    

    You can make it all a lot easier if you just use TThread, though. You can make your descendant class have as many parameters as you want in its constructor, so you don’t have to mess around with dynamically allocating and freeing a special record.

    type
      TCompareFilesThread = class(TThread)
      private
        FEdit3Text,
        FEdit4Text: string;
        FGrid: TStringGrid;
        FOp: Integer;
        procedure Execute; override;
      public
        constructor Create(const Edit3Text, Edit4Text: string; Grid: TStringGrid; Op: Integer);
        property ReturnValue;
      end;
    
    constructor TCompareFilesThread.Create;
    begin
      inherited Create(False);
      FEdit3Text := Edit3Text;
      FEdit4Text := Edit4Text;
      FGrid := Grid;
      FOp := Op;
    end;
    
    procedure TCompareFilesThread.Execute;
    begin
      ReturnValue := CompareFiles(FEdit3Text, FEdit4Text, FGrid, FOp);
    end;
    

    Instead of calling BeginThread, you just instantiate the class and let it run:

    var
      ThreadRef: TThread;
    
    
    ThreadRef := TCompareFilesThread.Create(Edit3.Text, Edit4.Text, StringGrid2, Op);
    

    There’s more to using threads, such as knowing when the thread has finished running, but I think you have enough to get started. One last thing to beware of, though, is that TStringGrid is a VCL control. You mustn’t do anything with it from this new thread you create (regardless of how you end up creating it). Eveything you do with the grid control need to be done from the main thread. Use TThread.Synchronize and TThread.Queue to shift any VCL operations onto the main thread. Your file-comparing thread will wait for the synchronized operation to complete, but it will keep running without waiting for a queued operation to complete.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to parse a xhtml file and display in UITableView. what is the
I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.