Solved

Trying to Resume a Thread

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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