Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Auto-start a new session from ServerController
#1
I want to auto-start a new session from ServerController that does not have a user interface.

Is there a better way of starting the session other than by calling the URL with the required parameters?

TIA
Reply
#2
Do you really need to create a session or just to do some background task once or regularly?
Reply
#3
(06-02-2023, 11:09 PM)Alexandre Machado Wrote: Do you really need to create a session or just to do some background task once or regularly?

It is multiple background tasks regulary
Reply
#4
Ideally you should create a background thread and run these tasks from it. If they can run sequentially, it's even better.

You can use our TIWTimedThread class that will handle this nicely:


Code:
uses
  IW.Common.Threads;

type
  TMyThread = class(TIWTimedThread)
  protected
    procedure DoExecute; override;
  end;

procedure TMyThread.DoExecute;
begin
  // do your stuff here
end;

// In your ServerController unit:

// Declare the thread as a field in our ServerController:
type
  TIWServerController = class(TIWServerControllerBase)
    procedure IWServerControllerBaseNewSession(ASession: TIWApplication);
    procedure IWServerControllerBaseConfig(Sender: TObject);
    procedure IWServerControllerBaseDestroy(Sender: TObject);
  private
    FMyThread: TMyThread;
  public
  end;

// Create and start it from the ServerController.OnConfig event:
procedure TIWServerController.IWServerControllerBaseConfig(Sender: TObject);
begin
  FMyThread := TMyThread.Create('My background thread', Interval_in_miliseconds);
  FMyThread.Start;
end;

// Finalize the thread in ServerController's OnDestroy event:
procedure TIWServerController.IWServerControllerBaseDestroy(Sender: TObject);
begin
  if Assigned(FMyThread) then   // always check because creation of ServerController may fail if something is wrong
    FMyThread.Finish;           // Finish will terminate and destroy the thread
end;

TIWTimedThread will call the DoExecute method at regular intervals, which is specified when you create the thread.

I recommend you to put the code of the thread in a separate unit. Also, when working with threads: If you need any database connectivity from within the thread, create an exclusive connection in the thread itself, don't try to "share" an external connection. 

I'll see if I can publish a demo showing an usage example like this.

Cheers
Reply
#5
(06-03-2023, 08:28 PM)Alexandre Machado Wrote: Ideally you should create a background thread and run these tasks from it. If they can run sequentially, it's even better.

You can use our TIWTimedThread class that will handle this nicely:


Code:
uses
  IW.Common.Threads;

type
  TMyThread = class(TIWTimedThread)
  protected
    procedure DoExecute; override;
  end;

procedure TMyThread.DoExecute;
begin
  // do your stuff here
end;

// In your ServerController unit:

// Declare the thread as a field in our ServerController:
type
  TIWServerController = class(TIWServerControllerBase)
    procedure IWServerControllerBaseNewSession(ASession: TIWApplication);
    procedure IWServerControllerBaseConfig(Sender: TObject);
    procedure IWServerControllerBaseDestroy(Sender: TObject);
  private
    FMyThread: TMyThread;
  public
  end;

// Create and start it from the ServerController.OnConfig event:
procedure TIWServerController.IWServerControllerBaseConfig(Sender: TObject);
begin
  FMyThread := TMyThread.Create('My background thread', Interval_in_miliseconds);
  FMyThread.Start;
end;

// Finalize the thread in ServerController's OnDestroy event:
procedure TIWServerController.IWServerControllerBaseDestroy(Sender: TObject);
begin
  if Assigned(FMyThread) then   // always check because creation of ServerController may fail if something is wrong
    FMyThread.Finish;           // Finish will terminate and destroy the thread
end;

TIWTimedThread will call the DoExecute method at regular intervals, which is specified when you create the thread.

I recommend you to put the code of the thread in a separate unit. Also, when working with threads: If you need any database connectivity from within the thread, create an exclusive connection in the thread itself, don't try to "share" an external connection. 

I'll see if I can publish a demo showing an usage example like this.

Cheers
Thanks Alexandre, 

Much appreciated
Reply
#6
I've just published a new demo showing how to do it:

https://github.com/Atozed/IntraWeb/tree/...skInThread
Reply
#7
Thank you Alexandre.

Much appreciated
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)