Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CreateAnonymousThread Usage?
#1
Greetings!
Using Tokyo 10.2.3
IW 14.2.7

I started a new "Test" object to do some proof of concept stuff work work.

Currently I have a REST server wrote that we use FMX iOS/Android apps talk to. Works great for past 2.5+ years.

I have code that works with XSuperJSON/XSuperObject and some custom code to wrap all communications
and to wrap up JSON into objects.  This works great too.


Sooooo... I took this existing code from my FMX app and put it into a VCL IW stand alone server program.

Since the calls may take upwards of 10-15 seconds to run while it hits up the REST server.. REST server hits up a database back end,
does its magic and sends back... I wanted to use a thread.

I have something that "works" but not 100% sure if this is the proper way.... or can be done better.

Code:
..
..
  private
    salesSearchRequest: TSalesSearchRequest;
  public
  end;

..
..
procedure TIWForm117.btnGoClick(Sender: TObject);
begin
  IWMemo1.Clear;

  salesSearchRequest := TsalesSearchRequest.create;
  salesSearchRequest.serveraddress := 'myserver.com';
  salesSearchRequest.username := '<username>';
  salesSearchRequest.password := '<password>;
  salesSearchRequest.VIN := e_VIN.text;   /// <---- on the form itself, user types in a vehicle VIN 

  tthread.CreateAnonymousThread(
    procedure
    begin
      salesSearchRequest.Execute; // this takes the request and sends it out the REST server and does the magic to take the JSON response and turn it into object code.
    end).Start; // start the AnonymousThread so not to stop the user interface.

  IWTimer1.Enabled := true; // kicks off an Async timer that checks every 500ms

end;

..
..

..



procedure TIWForm117.IWTimer1AsyncTimer(Sender: TObject; EventParams: TStringList);
var
  idx: integer;
begin
  if assigned(salesSearchRequest) then
  begin

    case salesSearchRequest.State of

      jRunning: IWMemo1.Lines.Add('Waiting...'); // the salesSearchRequest is still running... so spit something out to memo box

      jComplete: // the salesSearchRequest is "finished" running... so now do something.
        begin
          IWTimer1.Enabled := false; // turn off timer 
          for idx := 0 to salesSearchRequest.inventory.inventoryresults.Count - 1 do
          begin
            IWMemo1.Lines.Add(inttostr(idx + 1) + ' : ' + salesSearchRequest.inventory.inventoryresults[idx].guid + // inventory GUID / ID
              '   ' + salesSearchRequest.inventory.inventoryresults[idx].year + '   ' + // year of vehicle
              salesSearchRequest.inventory.inventoryresults[idx].model); // model of vehicle
          end;
         freeandnil(salesSearchRequest);
        end;
    end;
  end;
end;
So in this case... I kick of the anonymousthread and start an ASYNC timer to check the status of the salesSearchRequest object.

Not sure if using the timer is the "best" way... it works... but is there a better method?

After the salesSearchRequest.execute;     I can't do "iwtimer1.enabled:=true;"  ... as nothing happens... but if i hit F5 to refresh the page,
then the timer starts working.  I don't know how or if even, so i can do something like

Code:
 salesSearchRequest.execute; // do my REST call magic
 iwTimer1.enabled := true;
 webapplication.updatestuff;
).start;
or if there is way to something with tthread.Synchronize that will play happy with IW client and can get code to kick off then to update the screen via async?
Any ideas would help! Smile
As mentioned, what I have today works... but I figure better to check in to see if better/proper ways of doing this.
Reply
#2
There is an async/thread/update demo in our repo. Have you taken a look at that?
Reply
#3
(02-14-2019, 02:31 PM)kudzu Wrote: There is an async/thread/update demo in our repo. Have you taken a look at that?

I saw the one in the Demo with progessbar , which is somewhat similar to what I did... but I was hoping a better way as that Demo is pretty old I think?

But yah, it was IWTimer based also as wondering if better way of kicking things off.. but if this the only way/preferred way.. then so be it ... 

But will say it does work ok with the CreateAnonymousThread usage...
Reply
#4
You can have a look at IWMonitor. There is a demo here:

https://github.com/Atozed/IntraWeb/tree/...IWMonitor2

IWMonitor is a kind of "lightweight" timer with a few more options. The good thing about it is that it doesn't lock the user session session.

IWMonitor basically watches a WebApplication property (named "Status") for changes. Whenever Status change, it will trigger an event. When the status equals a "target" value it also trigger events and you can respond to that.

Please have a look and let me know if you need further help
Reply
#5
(02-14-2019, 09:41 PM)Alexandre Machado Wrote: You can have a look at IWMonitor. There is a demo here:

https://github.com/Atozed/IntraWeb/tree/...IWMonitor2

IWMonitor is a kind of "lightweight" timer with a few more options. The good thing about it is that it doesn't lock the user session session.

IWMonitor basically watches a WebApplication property (named "Status") for changes. Whenever Status change, it will trigger an event. When the status equals a "target" value it also trigger events and you can respond to that.

Please have a look and let me know if you need further help

IWMonitor did exactly what I needed.

Code:
  IWMonitor1.Enabled := true;

  tthread.CreateAnonymousThread(
    procedure
    begin
      salesSearchRequest.Execute;
      webapplication.Status.Value := 100;
    end).Start;

Code:
procedure TIWForm117.IWMonitor1AsyncTimer(Sender: TObject; EventParams: TStringList);
var
  idx: integer;
begin
  webapplication.Status.Value := 0;
  IWMonitor1.Enabled := false;
  if assigned(salesProSearchRequest) then
   begin
    doStuff;
    iwmemo1.lines.add('Finished');
   end;
end;

Knowing I can do that... makes things pretty interesting Smile

Since I use tthread.CreateAnonymousThread all the time now for iOS/Android usage in FMX, I make a huge fan of it.

Thanks for the heads up!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)