Auto-start a new session from ServerController

<< Click to Display Table of Contents >>

Navigation:  Forum >

Auto-start a new session from ServerController

Forum link

 


 

06-01-2023, 05:50 AM:

 

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

 


 

06-02-2023, 11:09 PM:

 

Do you really need to create a session or just to do some background task once or regularly?

 


 

06-03-2023, 06:01 AM:

 

(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

 


 

06-03-2023, 08:28 PM:

 

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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 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

 


 

06-03-2023, 08:53 PM:

 

(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;         &nbsp;&nbsp;// 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.

 

CheersThanks Alexandre, 

 

Much appreciated

 


 

06-04-2023, 02:45 AM:

 

I've just published a new demo showing how to do it:

 

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

 


 

06-05-2023, 12:14 AM:

 

Thank you Alexandre.

 

Much appreciated