Solved

form in a thread

Posted on 2006-07-01
4
197 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
[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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

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