[Delphi XE 7 UPD1 FireMonkey / Win32] TThread and WebService Help...

Hi,
I write this basic thread class:

{code}
  TWebServiceThread = class(TThread)
  private
    FOnExecute   : TNotifyEvent;
    FOnCompleted : TNotifyEvent;
    procedure Completed;
  protected
    procedure Execute; override;
  public
    procedure LetsGo;
    property OnExecute   : TNotifyEvent read FOnExecute   write FOnExecute;
    property OnCompleted : TNotifyEvent read FOnCompleted write FOnCompleted;
  end;


procedure TWebServiceThread.Completed;
begin
  if Assigned(FOnCompleted) then FOnCompleted(Self);
end;

procedure TWebServiceThread.Execute;
begin
  try
    CoInitialize(NIL);
    try
      FOnExecute(Self);
      Synchronize(Completed);
    finally
      CoUnInitialize;
    end;
  except
  end;
end;

procedure TWebServiceThread.LetsGo;
begin
  Execute;
end;
{code}

and use like:

{code}
      WSThread                 := TWebServiceThread.Create(True);
      WSThread.OnExecute       := WSStartExecute;
      WSThread.OnCompleted     := WSCompletedExecute;
      WSThread.FreeOnTerminate := False;
      tmrNetwork.Enabled := True;
{code}

then 

{code}
procedure TfrmMain.WSStartExecute(Sender: TObject);
begin
   tmrNetwork.Enabled := False;
// SOAP stuff
   Network.SOAPService.Users_Update;
//  ...
end;

procedure TfrmMain.WSCompletedExecute(Sender: TObject);
begin
  tmrNetwork.Enabled := True;
end;

procedure TfrmMain.tmrNetworkTimer(Sender: TObject);
begin
  WSThread.LetsGo;
  //CheckSynchronize();
end;
{code}

Everything works fine except when thread start and does its job in internet the application freezes until thread completion.
Has a solution ?

Thanks!
0
Aldo
1/30/2015 1:01:01 AM
embarcadero.delphi.firemonkey 4901 articles. 4 followers. Follow

4 Replies
1381 Views

Similar Articles

[PageSpeed] 0

Aldo wrote:

> procedure TWebServiceThread.LetsGo;
> begin
> Execute;
> end;

You can't call Execute() directly like that.  Since your thread is created 
in a suspended state, you need to use the TThread.Start() method instead:

{code}
procedure TWebServiceThread.LetsGo;
begin
  Start;
end;
{code}

> procedure TfrmMain.WSStartExecute(Sender: TObject);
> begin
> tmrNetwork.Enabled := False;

I am assuming that tmrNetwork is a TTimer, right?  Keep in mind that your 
OnExecute event handler is called in the context of the worker thread, but 
your OnCompleted event handler is called in the context of the main UI thread 
instead (because of your use of TThread.Synchronize()).  So you are (de)activating 
tmrNetwork across thread boundaries.  If tmrNetwork is indeed a TTimer, that 
will not work, because TTimer creates an internal HWND that has an affinity 
for the thread that creates it.  Only the thread that creates the HWND can 
destroy it without error.

> Everything works fine except when thread start and does its job
> in internet the application freezes until thread completion.

That is because you are calling Execute() directly, so it is running in the 
context of the calling thread, not in the context of the TThread worker thread 
like it should be.

-- 
Remy Lebeau (TeamB)
0
Remy
1/30/2015 1:01:01 AM
Hola Remy!


> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> Aldo wrote:
> 
> > procedure TWebServiceThread.LetsGo;
> > begin
> > Execute;
> > end;
> 
> You can't call Execute() directly like that.  Since your thread is created 
> in a suspended state, you need to use the TThread.Start() method instead:
> 
> {code}
> procedure TWebServiceThread.LetsGo;
> begin
>   Start;
> end;
> {code}

I try with .start but I cant... I receive a exception: "cannot call start on a running o suspended thread". :S



> > procedure TfrmMain.WSStartExecute(Sender: TObject);
> > begin
> > tmrNetwork.Enabled := False;
> 
> I am assuming that tmrNetwork is a TTimer, right?  Keep in mind that your 
> OnExecute event handler is called in the context of the worker thread, but 
> your OnCompleted event handler is called in the context of the main UI thread 
> instead (because of your use of TThread.Synchronize()).  So you are (de)activating 
> tmrNetwork across thread boundaries.  If tmrNetwork is indeed a TTimer, that 
> will not work, because TTimer creates an internal HWND that has an affinity 
> for the thread that creates it.  Only the thread that creates the HWND can 
> destroy it without error.

yes, I have to remove the timer and use "while/sleep" instead ?

> 
> > Everything works fine except when thread start and does its job
> > in internet the application freezes until thread completion.
> 
> That is because you are calling Execute() directly, so it is running in the 
> context of the calling thread, not in the context of the TThread worker thread 
> like it should be.

how fix if i receive "cannot call start on a running o suspended thread."? :(


> -- 
> Remy Lebeau (TeamB)
0
Aldo
1/31/2015 1:01:01 AM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> Aldo wrote:
> 
> > procedure TWebServiceThread.LetsGo;
> > begin
> > Execute;
> > end;
> 
> You can't call Execute() directly like that.  Since your thread is created 
> in a suspended state, you need to use the TThread.Start() method instead:
> 
> {code}
> procedure TWebServiceThread.LetsGo;
> begin
>   Start;
> end;
> {code}
> 
> > procedure TfrmMain.WSStartExecute(Sender: TObject);
> > begin
> > tmrNetwork.Enabled := False;
> 
> I am assuming that tmrNetwork is a TTimer, right?  Keep in mind that your 
> OnExecute event handler is called in the context of the worker thread, but 
> your OnCompleted event handler is called in the context of the main UI thread 
> instead (because of your use of TThread.Synchronize()).  So you are (de)activating 
> tmrNetwork across thread boundaries.  If tmrNetwork is indeed a TTimer, that 
> will not work, because TTimer creates an internal HWND that has an affinity 
> for the thread that creates it.  Only the thread that creates the HWND can 
> destroy it without error.
> 
> > Everything works fine except when thread start and does its job
> > in internet the application freezes until thread completion.
> 
> That is because you are calling Execute() directly, so it is running in the 
> context of the calling thread, not in the context of the TThread worker thread 
> like it should be.
> 
> -- 
> Remy Lebeau (TeamB)


Hi again Remy!

You are right!
I have the exception because I call start in a started thread, bad me!
I remove the timer, replace it with a loop while and sleep and all work like a charm!
You are the best!!

Another question, when I want terminate the thread, I call .Terminate correct?
Is normal delay thread finish?

thanks for all!
0
Aldo
1/31/2015 12:44:28 PM
Aldo wrote:

> Another question, when I want terminate the thread, I call .Terminate
> correct? Is normal delay thread finish?

All TThread.Terminate() does is sets the TThread.Terminated property to true. 
 It is the responsibility of your Execute() method to look at the Terminated 
property periodically and exit as soon as possible when the property is true. 
 The thread does not stop running until Execute() exits.

-- 
Remy Lebeau (TeamB)
0
Remy
1/31/2015 8:24:06 PM
Reply:

Similar Artilces:

Delphi 7 to Delphi XE
Have been using Delphi 7 for many moons ( have got later versions but never upgraded to ) My first problem is: Component Palette. in XE it is a small toolbar docked in top right in Delphi 7 it gives a large view of all the components. I am struggling to be able to cope/access my components.in Delphi XE. Can I make the component pallette tool bar the same size as Delphi 7, or is there a fast way to view/choose all available components in XE, that I have not spotted yet? Kind Regards, Robert. Hi, What I know is that in Delphi 2010 and XE you can choose between t...

Delphi XE 7 UPDATE 1 FireMonkey, Drag & Drop Files... how?
I have an app and want to add drag and drop files support, i try this code for example: {code} procedure TForm1.Grid1DragDrop(Sender: TObject; const Data: TDragObject; const Point: TPointF); begin caption := data.Files[0]; end; procedure TForm1.Grid1DragOver(Sender: TObject; const Data: TDragObject; const Point: TPointF; var Operation: TDragOperation); begin operation := TDragOperation.Link; end; {code} and work correctly in a new proyect, but if i implement this in an exisiting proyect this no work :( what happen? how make this work? thanks and sorry for my en...

Delphi XE 7 UPDATE 1 FireMonkey, Drag & Drop Files... how?
I have an app and want to add drag and drop files support, i try this code for example: {code} procedure TForm1.Grid1DragDrop(Sender: TObject; const Data: TDragObject; const Point: TPointF); begin caption := data.Files[0]; end; procedure TForm1.Grid1DragOver(Sender: TObject; const Data: TDragObject; const Point: TPointF; var Operation: TDragOperation); begin operation := TDragOperation.Link; end; {code} and work correctly in a new proyect, but if i implement this in an exisiting proyect this no work :( what happen? how make this work? thanks and sorry for my en...

Migrating to Delphi XE from Delphi 7.0
Below is my code in Delphi 7.0, this is how to call another units in webmodule... Hello All, I create a web application in Delphi 7.0, using the Web Server Application, CGI, IntraWeb 7.0.15. And I used TIWPageProducer to view like this url "http://localhost/mcr/mcr.exe/main". I built and run. I viewed in thru IIS and it is running... This is my code in Delphi 7.0 .... .... procedure TWebModule1.proMainGetForm(ASender: TIWPageProducer; AWebApplication: TIWApplication; var VForm: TIWPageForm); begin VForm := TfrmMain.Create(AWebApplication); end; procedure TWebModule1....

Migrate from Delphi 2007 for Win32 to Delphi XE
we use Delphi 2007 for Win32 to support legacy (32Bit) OWL-based pascal applications (yes i know it was a mistake not to switch to VCL 15 years ago). could our applications still be opened and compiled with Delphi XE? The existing projects are all plain Pascal-Code, coming back from the times of Turbo Pascal for Windows and later on Borland Pascal. Are there any improvements we could profit from (i.e IDE, Debugger)? Thanks Andrej > {quote:title=Andrej Dimic wrote:}{quote} > could our applications still be opened and compiled with Delphi XE? I'm not sure, but I guess ...

Migrating to Delphi XE from Delphi 7.0
Below is my code in Delphi 7.0, this is how to call another units in webmodule... Hello All, I create a web application in Delphi 7.0, using the Web Server Application, CGI, IntraWeb 7.0.15. And I used TIWPageProducer to view like this url "http://localhost/mcr/mcr.exe/main". I built and run. I viewed in thru IIS and it is running... This is my code in Delphi 7.0 .... .... procedure TWebModule1.proMainGetForm(ASender: TIWPageProducer; AWebApplication: TIWApplication; var VForm: TIWPageForm); begin VForm := TfrmMain.Create(AWebApplication); end;...

Delphi XE 5 / XE 6 Firemonkey custom component display problem
Hi everybody, I've a problem and I would like to know if you have encountered the same type of problem, and better, if you have solved it. Let's go for the explanation! With Firemonkey if you chose to create a new component derived from TListBox class with the wizard and you build and install it on a custom palet there is no problem. I mean that you can drag your own new component on a form and all is OK! But if you are doing the same thing with the TCustomListBox class as ancestor you can create your component without problem but when you drag this new one on a form you have ...

Delphi 7 to Delphi XE: TBlobField to XML [Edit]
Hi, I'm migrating a Delphi7 application to Delphi XE. I'm using a TClientDataSet to communicate, by using a XML frame, with my server. In this TClientDataSet I'm using a TBlobField which is an array of 384 byte. The blobField is allocate by a code like this : {code} myStream : TStream; myStream := aClientDataSet.CreateBlobStream(myBlobField, bmwrite); vResult := myStream.Write(ArrayOf384Byte[0], length(ArrayOf384Byte)); //vResult = 384 => GooD ! (...) {code} For communicate with the server, we have to decode the Blobfield in XML before to sending it. We have...

Win32 program: Delphi 7 vs Delphi XE5
How is a D7 Win32 program compared to a Delphi XE5 one in terms of stability and performance? Is Delphi XE5 good enough for a big ERP project with several DLL's and hundreds of units and forms? Thanks in advance Am 26.12.2013 15:02, schrieb lior ilan: > How is a D7 Win32 program compared to a Delphi XE5 one in terms of stability > and performance? > Is Delphi XE5 good enough for a big ERP project with several DLL's and > hundreds of units and forms? > Thanks in advance > Hello, XE5 has increased functionality. Stability seems to be ok for most ...

Delphi 7.0 code convert to delphi XE ...
Hello All, I create an application using Web Server Application then CGI stand alone... In WebModule I add ModuleController component and IWPageProcedure... Below is my code in Delphi 7.0, this is how to call another units in webmodule... .... .... procedure TWebModule1.proMainGetForm(ASender: TIWPageProducer; AWebApplication: TIWApplication; var VForm: TIWPageForm); begin VForm := TfrmMain.Create(AWebApplication); end; procedure TWebModule1.proLogInGetForm(ASender: TIWPageProducer; AWebApplication: TIWApplication; var VForm: TIWPageForm); begin VForm := TfrmLogIn.Create(AWebA...

How to access Delphi 7 & 2009 after purchasing Delphi XE
I have purchased and installed Delphi XE and now I'm unable to find information on how to download previous versions of Delphi, namely Delphi 7 and 2009. Please respond. Regards, SB, L&T Hi sushweta biswas, > I have purchased and installed Delphi XE and now I'm unable to find information on how to download previous versions of Delphi, namely Delphi 7 and 2009. Your reseller should have told you: after registering Delphi XE, you can go to http://www.embarcadero.com/xe-earlier-versions to enter your serial number of Delphi XE as well as your EDN account informati...

Win32 Delphi language features introduced since Delphi 7
Hi, Am I right in thinking that the language features introduced since Delphi 7 fall into the categories: a) language features dictated by .Net compatibility. e.g. Namespaces, Inlining, records with methods, operator overloading, pure interfaces, generics, extended RTTI and reflection; b) Unicode strings and supporting procedures? c) 64-bit support What other language features, if any, have been introduced since D7? Had most of the post-D7 languages features, except for generics, Unicode strings, and 64-bit support, been introduced in or before Delphi 2005? How bug-free were ...

Problem upgrading web service client from Delphi 7 to Delphi XE
In trying to upgrade our web service client to Delphi XE we have now get a web service error of Invalid Format. When we examine the SOAP request we see the following differences from Delphi 7 vs Delphi XE In Delphi 7 we get <SomeStuff xmlns="" xsi:nil="true" /> In Delphi XE we get <SomeStuff xsi:nil="true" /> We have narrowed the failure down to these differences in the client request. Is there anyway to get Delphi XE to generate a SOAP request with the xmlns attribute set to blank as it used to be? Hello, > In trying to u...

Need help to allow a working Delphi 3 project to build on Delphi XE
How do I adjust this working Delphi 3 program that uses OLEAUTO and OLE2 to work on the newer Delphi XE, Program code is below this, errors are here : Checking project dependencies... Compiling admn_api.dproj (Debug, Win32) dcc command line for "admn_api.dpr" c:\program files (x86)\embarcadero\rad studio\8.0\bin\dcc32.exe -$O- -$W+ --no-config -M -Q -AWinTypes=Windows;WinProcs=Windows;DbiTypes=BDE; DbiProcs=BDE;DbiErrs=BDE -DDEBUG -I"c:\program files (x86)\embarcadero\rad studio\8.0\lib\Win32\release";"C:\Users\Administrator\Documents\RAD Studio...

Web resources about - [Delphi XE 7 UPD1 FireMonkey / Win32] TThread and WebService Help... - embarcadero.delphi.firemonkey

Firemonkeys - GamesIndustry International
The world's leading games industry website. Get insight from todays industry leaders with news, interviews and analysis of global gaming trends. ...

Firemonkey’s Real Racing 3 To Launch At The End Of February
In September, Apple demoed Firemonkey’s Real Racing 3 at the iPhone 5 event . Three months later and the game has yet to show up in the App Store. ...

EA Games and Firemonkey Bringing Real Racing 3 to Android, Fasten your Seat Belts
Start up your engines race fans, EA Games is teaming up with Firemonkey to bring Real Racing 3 to mobile devices. If you’re a fan of more realistic ...

EA's Firemint and IronMonkey Studios Merge to Become FireMonkeys
... game development studios into one mega-studio in Australia. Firemint and IronMonkeys will be merged into a single studio now known as Firemonkeys ...

News: Firemonkeys announces Real Racing 3
Firemonkeys, a new gaming subsidiary of Electronic Arts born from the merger of FireMint and IronMonkey, has announced the coming release of ...

firemonkeys - iMore
EA has pushed out another impressive update to its equally impressive iOS racer, Real Racing 3, that for the first time brings cars from Ferrari ...

Firemonkeys on Real Racing 3 going free-to-play
... got a hands-on preview of Real Racing 3. We also spoke with Ptolemy Oberin, one of the game’s programmers and project lead at developer Firemonkeys, ...

Real Racing 3 coming in 2012 from Firemonkeys
The first game from recently merged developer Firemonkeys is Real Racing 3 , the developer revealed moments ago during EA's Summer Showcase event ...

Firemonkeys Previews Real Racing 3 for iPhone and iPad
Firemonkeys, the new combined studio from EA combining the IronMonkey and Firemint gaming studios, has announced the development of Real Racing ...

EA Mobile Moves: IronMonkey & Firemint Merge Into “Firemonkeys,” Now Have 50M Players Between
... that it is merging two top mobile game studios, IronMonkey and Firemint , which will fittingly combine to create a new company, called Firemonkeys. ...

Resources last updated: 12/12/2015 4:39:29 AM