Solved

Trying to Resume a Thread

Posted on 2004-07-30
2
534 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 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