?
Solved

Example Thread

Posted on 2003-03-17
5
Medium Priority
?
347 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

621 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