Solved

form in a thread

Posted on 2006-07-01
4
191 Views
Last Modified: 2010-04-05
I need some codes for 2 forms run simutaniously.
One(a) is main form. Another(b) is a form to monitor some system status. when (b) get the pc system  information, it will exchange data with (a), (a) will display some data on itself.

What is the solution for this, does it need put (b) in a thread?

Welcome any comment.

Thanks

Z W
0
Comment
Question by:wangzheng
4 Comments
 

Accepted Solution

by:
SergiuAdrian earned 125 total points
ID: 17025513
if you want to display the pc system info on form (a) only, you don't have to create another form (b) if you don't use it to display anything.

just create another unit and make a thread class. here is some basic code for it:

----------------------------------------------------------------------------------------
unit SomeUnitWithThread;

interface

uses
  Classes;

type
  TGetInfoThread = class(TThread)
  private
    SomeInfo : string;
    { Private declarations }
  protected
    procedure Execute; override;
    function GetSomeInfo() : string;
  end;

implementation

procedure TGetInfoThread.Execute;
begin
  { some code to get PC info. don't forget to check
    if the thread is terminated and stuff. to learn how
   to use threads see delphi's help or search the internet,
   for example here:  

   http://delphi.about.com/od/kbthread/index.htm?terms=threads+delphi

  }
end;

function TGetInfoThread.GetSomeInfo():string;
begin
   result := SomeInfo;
end;
end.
----------------------------------------------------------------------------------------

you then make a thread object in the main form:

--------------------------------------
var Form1 : TForm1;
     GetPcInfo: TGetInfoThread;
.........
//now instantiate after the main form is created
procedure TForm1.FormCreate(Sender: TObject);
begin
GetPCInfo := TGetInfoThread.Create(false);// false means the thread is running,
                                                               //true means the thread is suspended after the creation.
.......
--------------------------------------

of course, you can declare the TGetInfoThread class in the same unit with the main form, but i like to have the threads in different units.

0
 
LVL 17

Assisted Solution

by:TheRealLoki
TheRealLoki earned 125 total points
ID: 17028024
As long as both forms aren't doing tight loops, you don't have to do anything.
eg. If you are using TTImer's to do the checking, or you are doing "Application.ProcessMessages" in any tight loops, so that both forms can continue to process.

Here's an example just using timers
make sure unit2 is in unit1's uses lise, and unit1 is in unit2's uses list (the uses under implementation)
so that you don't get the  cyclic reference issue.
e.g.

unit Unit1;

interface
uses forms,... etc....   , unit2;

implementation

procedure Form1Timer1Timer(sender: TObject);
begin
    timer1.enabled := false;
    try
        form2.TimeLabel.Caption := TimeToStr(now);
    finally
        timer1.enabled := true;
    end;
end;


unit Unit1;

interface
uses forms, ....etc;

implementation

uses unit1;

procedure Form2Timer2Timer(sender: TObject);
begin
    timer2.enabled := false;
    try
        form1.DateLabel.Caption := DateToStr(now);
    finally
        timer2.enabled := true;
    end;
end;




0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

13 Experts available now in Live!

Get 1:1 Help Now