Solved

Using threads

Posted on 1998-06-07
4
142 Views
Last Modified: 2010-05-18
I need to write a simple program that uses another thread..
I've done this in C++ builder without any problems, but I seem to be getting something wrong doing this with Delphi..

Here's some of my code:
type
  newthread = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
  public
    procedure formrepaint;
  end;

implementation
uses Unit1;

procedure newthread.Execute;
var
cont : boolean;
begin
cont := true;
  while (cont) do
  begin
   Synchronize(Formrepaint);
   if terminated then
    cont := false;
  end;
end;

procedure newthread.formrepaint;
begin
mainform.repaint;
end;

now, the problem I'm having is somewhere at the other end (I think)... in mainform, I have the code
var
  other : newthread;
...
other.create(false);
...
{here I have some code that accesses and does some changes to the mainform}
...
other.destroy;

I'm certain that my mistake is a small and stupid one that would be obvious to someone who know how to use threads in Delphi... please HELP.

The program always crashes where I call the create method... what am I missing????

Thanks in advance...
0
Comment
Question by:rafy
[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
4 Comments
 
LVL 5

Expert Comment

by:julio011597
ID: 1351256
Can yo show your exact error message... or whatever you get upon calling create()?

Also, there seems to be no reason for such a behaviour in the code you've posted... is that all?

Regards, julio
0
 

Author Comment

by:rafy
ID: 1351257
The error I'm getting is a
"Access violation at address..."
Do I have to define create??

Note: I also get a compiler warning telling me that variable "other" may not be initialized (?)
0
 
LVL 5

Accepted Solution

by:
inter earned 100 total points
ID: 1351258
Hi there,
The problem is basic and nothing to do with threads. Any pascal class INSTANCE can not be created as
    ...
    other.create(false);
    ...
the correct way is
    other := newthread.Create(false);
Upon destruction of the thread call Terminate first then Free(not Destroy it is an ill practice for VCL.)

   other.terminate;
   other.free;
Regards, Igor
0
 

Author Comment

by:rafy
ID: 1351259
Perfect!... I was so certain it was something simple like that... Thanks again
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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