Solved

Using threads

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 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 video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

17 Experts available now in Live!

Get 1:1 Help Now