Solved

Trying to Resume a Thread

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

14 Experts available now in Live!

Get 1:1 Help Now