Solved

Using threads

Posted on 1998-06-07
4
135 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect!... I was so certain it was something simple like that... Thanks again
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

11 Experts available now in Live!

Get 1:1 Help Now