Solved

Threads and Forms

Posted on 1998-09-30
13
221 Views
Last Modified: 2010-04-06
I have a program using two forms, a controlform and a displayform. The displayform contains a Thread form that does some calculations and then draws onto the canvas of an image in the diplayform (which is not visible). As soon as it does this the thread does not perform any further operations. If I replace the thread by a regular procedure the whole thing works fine, exceipt for the fact that the calculations take a while, and the program does not respond during that time.

Any ideas? Thanks in advance.
0
Comment
Question by:simondittami
  • 6
  • 2
  • 2
  • +3
13 Comments
 
LVL 1

Expert Comment

by:pjdb
ID: 1341325
do you create the thread with MyThread.Create(True) ? If yes, then you need to call MyThread.Execute or anything of this kind.
If you create the thread with MyThread.Create(False);, your caclulation need to be in the execute procedure.
in any case, do not forgot the override; after lthe exectue procedure declaration and the inherited execute; in it's execution.

JDB
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1341326
Use Application.ProcessMessages;

Regards,
Viktor Ivanov
0
 

Author Comment

by:simondittami
ID: 1341327
That does not seem to solve the problem, wherever I put it. Perhaps you could be more specific about how I should use it.

Thanks anyway, Simon

As for JBD: I use MyThreaclass.crate(false); MyThreadclass.execute is followed by override; in the head of the unit
It contains the calculation, which also seems to work fine, until it starts setting Pixels on the canvas of an image in the Displayform.
I'm a newbie in delphi, and I'm afraid I don't know what the inherited Execute is!

Any further help would be greatly appreciated.

Simon
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1341328
Insert that in the Execute method or where you go the big procedure that slows down your app so it doesn't respond.////

//Vik
0
 

Author Comment

by:simondittami
ID: 1341329
I've tried, but it does not seem to solve my problem. The programm still seems to crash.

Thanks for trying!
0
 

Author Comment

by:simondittami
ID: 1341330
Adjusted points to 105
0
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.

 

Expert Comment

by:XiaoChen
ID: 1341331
Use Win API CreateThread
and do the calculation in the new thread.
when this calculating thread ends,
save result to a global memory zone and
send a message to one of your window
to notify the display form refresh the content
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1341332
hi simon,

give me me your email address, i will send you the source of a little application which use threads and forms as sample.

meikl
0
 

Author Comment

by:simondittami
ID: 1341333
This would of course work, but unfortunately the "result" is  a 40 MB image, having 2 40 MB images in my RAM or on the harddisk is what I'm trying to avoid. (if you know a way of avoiding that please anwer again.

Thank you, though.


0
 

Author Comment

by:simondittami
ID: 1341334
ok - sounds good. My email is sdittami@degnet.baynet.de

if that works, you get the points.
0
 
LVL 1

Accepted Solution

by:
bcrotaz earned 130 total points
ID: 1341335
No no no....
The clue was in an earlier mail when you said that the calculation worked, but as soon as you drew pixels, it stopped working.

The problem is that the Delphi VCl is not thread safe.  In a nut-shell, it means that if you call and VCl object methods from inside a thread object, you must Synchronize them.  What that means in actual fact is that (behind the scenes) the thread object sends a Windows message to the VCL, telling it to call a method in your thread.  This means that the method is executed in the context of the VCL thread, not your thread.  Confusing, isn't it!  This should make it clear:

  TMyThreadObj = Class(TThread)
    private
      FSomeData: String; // this data is required by your draw routine
    protected
      procedure Execute; override;
      procedure DoDraw;
  end;

procedure TMyThreadObj.Execute;
begin
  // Do Calculation here
  // When you need to draw a pixel
  // Set the private data for the pixel location and color etc
  FSomeData:='Here goes the data';
  // Now call the DoDraw method via Synchronize
  Synchronize(DoDraw); // easy, isn't it!
end;

procedure TMyThreadObj.DoDraw;
begin
  // This must always be called via Synchronize
  MainForm.DrawPixelsMyWay; // do your drawing here
end;
0
 

Author Comment

by:simondittami
ID: 1341336
Yeah - That's Great.
---- Thank you. The points are yours.

I still wonder why Application.ProcessMessages did not work and also why my program runs faster now than it did before without threads ???

Best whishes, Simon
0
 
LVL 1

Expert Comment

by:bcrotaz
ID: 1341337
Because Application.ProcessMessages goes away to process all messages from the mouse, keyboard, disk etc, then comes back to your code.  And while your code is waiting for the disk or the screen, nothing happens.  With threads, the other bits of code can be executing while another thread is waiting on a resource.

Just be careful about synchronisation.  It's something that has to be designed in.  If you try to do without, then add it, your code will randomly lock up, as you get race conditions and deadlocks appearing.  There's a couple of very good books on the subject.  Go to Amazon.com and search for 'win32 threads'.  Be aware that the examples are in C, but they're very clearly commented, so you can see the theory without having to understand the language (I don't read C).  It's nt an easy subject to get your head round, but well worth doing so.  It's taken me about 2 months to understand them, and my software package now has more than ten simultaneous threads.
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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 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

15 Experts available now in Live!

Get 1:1 Help Now