Solved

Trying to Resume a Thread

Posted on 2004-07-30
2
529 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
Comment Utility
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
Comment Utility
That was indeed the problem. Thanks a bunch!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

19 Experts available now in Live!

Get 1:1 Help Now