Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That's not <em>at all</em> the way to use <code>BeginThread</code>. That function expects a pointer to a function that takes <em>one</em> parameter, but the function you're trying to call wants four. The one parameter you're giving to <code>BeginThread</code> 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.</p> <p>That's not how Delphi works, and even for the languages that <em>can</em> do something like that, it's generally discouraged to actually <em>do</em> it.</p> <p>To pass multiple parameters to <code>BeginThread</code>, define a record with all the values you'll need, and also define a record pointer:</p> <pre><code>type PCompareFilesParams = ^TCompareFilesParams; TCompareFilesParams = record Edit3Text, Edit4Text: string; Grid: TStringGrid; Op: Integer; end; </code></pre> <p>Change <code>CompareFiles</code> to accept a pointer to that record:</p> <pre><code>function CompareFiles(Params: PCompareFilesParams): Integer; </code></pre> <p>To start the thread, you'll need to allocate an instance of that record and populate its fields:</p> <pre><code>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); </code></pre> <p>Implement <code>CompareFiles</code> like this so that the record will get freed before the thread terminates:</p> <pre><code>function CompareFiles(Params: PCompareFilesParams): Integer; begin try // &lt;Normal implementation goes here.&gt; finally Dispose(Params); end; end; </code></pre> <p>You can make it all a lot easier if you just use <code>TThread</code>, 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.</p> <pre><code>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; </code></pre> <p>Instead of calling <code>BeginThread</code>, you just instantiate the class and let it run:</p> <pre><code>var ThreadRef: TThread; ThreadRef := TCompareFilesThread.Create(Edit3.Text, Edit4.Text, StringGrid2, Op); </code></pre> <p>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 <code>TStringGrid</code> 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 <code>TThread.Synchronize</code> and <code>TThread.Queue</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload