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
Solved

Using threads

Posted on 1998-06-07
4
140 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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Title # Comments Views Activity
Making delphi communicate with a c# service 16 111
select query - oracle 16 100
can't find the executable in Simulator 1 100
creating threads in delphi 1 131
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 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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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