?
Solved

Using threads

Posted on 1998-06-07
4
Medium Priority
?
144 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 400 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

770 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