TThread WaitFor

I'm trying to figure out the useage of certain TThread methods.

Currently, i have 4 checkboxes, and a button.

on button click event i do

if CheckBox1.Checked then
begin
 mode := 1;
 thread.create(false);
end;

if CheckBox2.Checked then
begin
 mode := 2;
 thread.create(false);
end;

etc...

this doesn't seem to work out all that well.

how would i either

1) wait for each of the if statements to finish before moving onto the next one

or

2) wait for the thread to finish then move onto the next if statement..

i set globals in the if statements, as you can see, so unless there is a way to pass these in the call, how would you do that?

thanks!
gwarguitarAsked:
Who is Participating?
 
jturpinCommented:
If you want the thread to finish before conitinuing then why not forget about the thread and just put the code inline?

Each if statement will finish before moving on to the next one: If the checkbox is checked then the process thread (the main program) will launch the thread (lets call it threadA)  and the process thread will move on to the next if statement. ThreadA will run when it gets a time slice.

Note, Mike is right. You do need to create the thread as he suggests.

Regarding the global variable you can pass it to the thread as a parameter. If you want to pass by reference you will need to set up critical sections inside the thread to access it. Otherwise pass itby value. To pass by value do this:

TMyThread = class(TThread)
  private
    myMode : Integer;
  public
    constructor Create(ThisMode:Integer; ThisCreateSuspended:Boolean);
end;

constructor TMyThread.Create(ThisMode:Integer; ThisCreateSuspended:Boolean);
begin
  inherited Create(true);
  myMode:= ThisMode;
  if (not ThisCreateSuspended) then
    resume;
end; {Create}


 
0
 
mikelittlewoodCommented:
I have never used threads before,  and Im SURE someone will be able to tell you how to do it, but I just wanted to ask if that is how you actually create a thread correctly.

Is it not thread := TThread.Create(False) ?

This seems to be a popular thread information source so hopefully you might be able to gleam some information from here
http://www.midnightbeach.com/jon/pubs/MsgWaits/MsgWaits.html

gl
Mike
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.