Solved

please help me thread my app.

Posted on 2004-09-22
5
148 Views
Last Modified: 2010-04-05
I have an app that would VASTLY benefit from 30+ threads.   It is on one form.  I have a start and stop button that set "stopper" var.   While stopper is false, the app will fire one of 2 procedures.  These are nearly identical.    One uses one dataset (clientdataset1) and the other users 2 datasets (clientdataset1 and clientdataset2)  I will only be using one at a time and never in the same session.  I personally have never used threads and get severely confused when reading about them as the verbage is unfamiliar to me.  the issues that would seem to be tough are teh dataset access and the reporting of status to the main form.  

Please give as detailed an answer as you can, although I appreciate that this is not an easy question.

Andrew
0
Comment
Question by:hibbidiji
5 Comments
 
LVL 2

Expert Comment

by:__alex
ID: 12130451
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12130529
if the DataSet from the first thread is one of the DataSets in the second thread ... you have to use CriticalSection...
0
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 500 total points
ID: 12130550
// THREAD EXAMPLE
// -----------------------------

TTestThread = class(TThread)
private
  LocalDataSet   : TClientDataSet;
public
  IsFinished     : Boolean;
  constructor Create(CreateSuspended : Boolean; DS : TClientDataSet);
  procedure Execute; override;
end;


constructor TTestThread.Create(CreateSuspended: Boolean;
  DS: TClientDataSet);
begin
  // create the thread
  inherited Create(CreateSuspended);
  // pass the dataset locally
  LocalDataSet := DS;
  // init
  IsFinished := False;
end;

procedure TTestThread.Execute;
begin
  inherited;
  // Access the DS here
  // do something with it here...

  IsFinished := True;
end;

// EXAMPLE HOW YOU CALL IT
// ----------------------------------

procedure TForm1.Button1Click(Sender: TObject);
var
  tr      : TTestThread;
begin
  tr := TTestThread.Create(False, ClientDataSet1);
  while not tr.IsFinished do
    Application.ProcessMessages;
end;
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12133936
1)DataSets

i think better way is to work with 3 datasets

thread A: clientdataset1A
thread B: clientdataset1B and clientdataset2B

this way you do not need to use critical sections

2)reporting of status to the main form

you can use
   PostMessage(MainForm.Handle, YOUR_MESSAGE, wParam, lParam)



0
 
LVL 1

Author Comment

by:hibbidiji
ID: 12139458
hard but not impossible. thanks!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

22 Experts available now in Live!

Get 1:1 Help Now