Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Threads and Forms

Posted on 1998-09-30
13
Medium Priority
?
230 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 

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 520 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

705 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