?
Solved

Trying to Resume a Thread

Posted on 2004-07-30
2
Medium Priority
?
535 Views
Last Modified: 2010-04-04
First time playing with multithreading, and i'm having a hard time with something that should be simple, always the way.

I was able, through much trial and error, to SUSPEND a running thread, now i'm trying to RESUME it, code snippet:

type
tsensormon = class(TThread)
private
protected
procedure Execute; override;
procedure checkpressure;
public
constructor create;
end;

constructor tsensormon.create;
begin
inherited create(true);
end;

the form create procedure, THAT resume works perfectly fine:

procedure TMainForm.FormCreate(Sender: TObject);
var
sensormon : tsensormon;
begin
ProgramShutDown := False;
sensormon := Tsensormon.Create;
sensormon.FreeOnTerminate := True;
sensormon.resume;
end;

the execute (doesn't really do anything, just fill a memo with characters for testing purposes:

procedure tsensormon.Execute;
var
I : integer;
begin
for I := 0 to 10000 do
begin
mainform.Memo1.lines.add(inttostr(I));
if suspendthreadflag then suspend;
end;
end;


this sets suspendthreadflag when you click a button:

procedure TMainForm.Button3Click(Sender: TObject);
begin
suspendthreadflag := true;
end;

this SUPPOSEDLY resumes the thread:

procedure TMainForm.Button4Click(Sender: TObject);
var
sensormon : tsensormon;
begin
suspendthreadflag := false;
sensormon.Resume;
end;

When that last procedure is run, i get : Project TERMDEMO.exe raised exception class EThread with message 'Thread Error: The handle is invalid (6)'. Process stopped. Use Step or Run to continue.

I'm using Delphi 7.
0
Comment
Question by:Nailhead
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
rbohac earned 2000 total points
ID: 11680823
You are losing scope of your thread variable. Because you declare procedure sensormon : tsensormon; inside of your oncreate procedure, you lose access to that variable after oncreate is complete.

Make this a global variable.

var sensormon : tsensormon;

procedure TMainForm.FormCreate(Sender: TObject);
begin
ProgramShutDown := False;
sensormon := Tsensormon.Create;
sensormon.FreeOnTerminate := True;
sensormon.resume;
end;

procedure TMainForm.Button4Click(Sender: TObject);
begin
suspendthreadflag := false;
if assigned(sensormon) then
  sensormon.Resume;
end;
0
 

Author Comment

by:Nailhead
ID: 11680961
That was indeed the problem. Thanks a bunch!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 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