#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Basic multi thread example


    Hi,

    I've been scouring the internet for a specific example but am having no luck. I doubt this is too much of a challenge for someone.
    What I need is an example where the VCL passes a start and end number to a thread. The thread then loops from start to finish, and everytime it gets to a number divisible by 10, it passes back "hello world" to a listbox on the original form.
    The application must still be useable during the thread loop so I can continue opening other forms.

    Basic i know, but I'm having issues

    Delphi version 2006
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    354
    Rep Power
    7
    You've mentioned that you've searched for examples, but don't know if you've read this (rather lengthy) article on how threads work in Delphi. Hopefully this will help you..

    http://www.eonclash.com/Tutorials/Multithreading/MartinHarvey1.1/ToC.html
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0
    Hi there and thanks for the reply.
    Yes I have read through this tutorial and it is very good.

    The issue I have is that the demo he works through processes an entire loop within the thread before passing back the full results.

    What I would like it something that passes a single entry in the result list as it goes back to the VCL before continuing in its loop.

    Maybe someone can alter the example he has, otherwise maybe someone can point out what part of the code I might need to change.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    354
    Rep Power
    7
    First, passing values to your thread is rather simple, you just need to declare a new constructor for your thread class, and have a place to hold these values within your thread, and to update your main thread, you need a parameterless procedure you can call with synchronize:

    Assuming your thread's unit is called MyThreadUnit, and your thread is called TMyThread:
    Code:
    type
       TMyThread = class(TThread)
       private
          fLowerLimit: Integer;
          fUpperLimit: Integer;
          procedure UpdateMainThread;
       protected
          procedure Execute; override;
       public
          constructor Create(lValue, uValue: Integer; Suspended: Boolean);
       end;
    
    implementation
    
      uses MainForm; //Unit of the form that will be updated by thread
    
    constructor TMyThread.Create(lValue, uValue: Integer; Suspended: Boolean);
    begin
       Inherited Create(Suspended);
       fLowerLimit := lValue;
       fUpperLimint := uValue;
       FreeOnTerminate := True;
    end;
    
    procedure TMyThread.Execute;
    var
       I: Integer;
    begin
       For I := fLowerLimit to fUpperLimit do
          if (I mod 10) = 0 then
             Synchronize(UpdateMainThread);
    end;
    
    procedure TMyThread.UpdateMainThread;
    begin
       MainForm.Memo1.Lines.Add('Hello World');
    end;
    
    end.
    Then for the form that will create your thread and needs to be updated by your thread, place a button on the form, and a memo control. Add your thread to the uses clause and add a private variable of your Thread's class

    Code:
    type
       TForm1 = class(TForm)
          memo1: TMemo;
          Button1: TButton;
          procedure Button1Click(Sender: TObject);
       private
          CountingThread: TMyThread;
       end;
    
    implementation
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
       CountingThread := TMyThread.Create(22, 148, True)
       CountingThread.Resume;
    end;
    
    end.
    Last edited by majlumbo; March 7th, 2012 at 08:12 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0
    Hi,

    The main issue with this example is that it doesn't leave the UI useable. If I run this loop to 1000, I'm stuck until it finishes.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    354
    Rep Power
    7
    Originally Posted by Boicey
    Hi,

    The main issue with this example is that it doesn't leave the UI useable. If I run this loop to 1000, I'm stuck until it finishes.

    You're dealing with a delay because of the Synchronize method, if you want to minimize that, then another approach could be to define a user message that your application can respond to, and have your thread send that message.

    You can look here on how to define a WM_USER message.

IMN logo majestic logo threadwatch logo seochat tools logo