?
Solved

Example Thread

Posted on 2003-03-17
5
Medium Priority
?
344 Views
Last Modified: 2010-04-04
Hi,

I work with Delphi 6.

I have an application desktop (Client) that it has one timer.
In the execution of the timer is calling the Web Services (Server) - BabelFishService.
The communication between clients and servers is made through the
SOAP(Simple Object Access Protocol).

I have un update of the clock with a frequency of half seconds and this work very well.

The problem:
If I write a great text in English and make the translation the update of the clock stop (freeze).

I need:
To change  the "procedure Translate"  to one thread.

Please somebody can change the current source for another equal one, but using "thread"?


procedure TForm1.FormShow(Sender: TObject);
begin
  Timer1Timer(Sender);
  Translate;
  Timer1.Enabled  := True;
end;

procedure TForm1.Translate;
var  BF: BabelFishPortType;
begin
   Screen.Cursor:= crHourGlass;
  try
    { Web Services }
    BF:= HTTPRIO1 as BabelFishPortType;
    Memo2.Text:= BF.BabelFish('en_pt', memo1.Text);
  finally
    Screen.Cursor:= crDefault;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  MaskEdit1.Text := TimeToStr(Time);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TForm1.btbTranslateClick(Sender: TObject);
begin
  Translate;
end;
0
Comment
Question by:faustomen
[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
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:DaFox
ID: 8155153
Hi.

type
  TTimeThread = class(TThread)
    procedure Execute; override;
    procedure SetTime;
  public
    constructor Create(CreateSuspended: Boolean);
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    MaskEdit1: TMaskEdit;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
  public
  end;

var
  Form1: TForm1;
  TimeThread: TTimeThread;

constructor TTimeThread.Create(CreateSuspended: Boolean);
begin
  inherited Create(CreateSuspended);
  FreeOnTerminate:=True;
end;

procedure TTimeThread.Execute;
const
  Delay = 1000; // 1 sec
var
  FTickCount: Integer;
begin
  FTickCount := GetTickCount + Delay;
  while (not Terminated) do
  begin
    if (GetTickCount >= FTickCount) then
    begin

      Synchronize(SetTime);

      FTickCount := GetTickCount + Delay;
    end else Sleep(50);
  end;
end;

procedure TTimeThread.SetTime;
begin
  Form1.MaskEdit1.Text := TimeToStr(Time)
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  TimeThread := TTimeThread.Create(false);
  Translate;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  TimeThread.Terminate;
  Action := caFree;
end;

// ...


Hope that helps,
Markus
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8155267
I think DaFox this will not work as Synchrnoze posts an execute message to form message queue (same does WM_TIMER) :(
translating procedure must be placed in thread.

  TForm1 = class(TForm)
    ...
    Label1: TLabel;
    Label2: TLabel;
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    procedure ThreadTerminate(Sender: TObject);
    procedure Translate;
  end;

implementation

type
  TBabelThread = class(TThread)
  private
    FText: String;
    FBF: BabelFishPortType;
  public
    property BF: BabelFishPortType read FBF write FBF;
    property Text: string read FText write FText;
    procedure Execute; override;
  end;

procedure TBabelThread.Execute;
begin
  FText:= FBF.BabelFish('en_pt', FText);
end;

procedure TForm1.Translate;
begin
  label2.Caption := 'Translating...';
  with TBabelThread.Create(True) do
  begin
    OnTerminate := ThreadTerminate;
    Text := memo1.Text;
    BF := HTTPRIO1 as BabelFishPortType;
    Resume;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  label1.Caption := TimeToStr(Now);
end;

procedure TForm1.ThreadTerminate(Sender: TObject);
begin
  Memo2.Text := TBabelThread(Sender).Text;
  Label2.Caption := 'Done';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Translate;
end;

wbr, mo.
0
 

Author Comment

by:faustomen
ID: 8160681
Hi Mocarts,

I copied the code and made test, but it does not work.

The call   "FText:= FBF.BabelFish('en_pt', FText);"   in the Web Service does not work.

I changed the call "FText:= FBF.BabelFish('en_pt', FText);"   to   "Select * from table_database"  and  it is work (database local).

Why the call in the Web Services does not work?

Can you help me again?
0
 
LVL 9

Accepted Solution

by:
mocarts earned 2000 total points
ID: 8161841
procedure TBabelThread.Execute;
begin
 CoInitialize(nil);
 try
   FText:= FBF.BabelFish('en_pt', FText);
 finally
   CoUninitialize;
 end;
end;

CoInitialize must be called for each thread in order to use COM library. I didn't konw that as I used COM interface in thread first time.

wbr, mo.
0
 

Author Comment

by:faustomen
ID: 8166126
Hi Mocarts,

Really works.

Thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

764 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