Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Close Remote Session
#1
Hi!

Every time an user make loggin in the application, the AppId (sessionId) is save in the database.

With this information, I try to terminate the session from another program and my code fail every time.

How can I end the session?

Delphi Seattle
Intraweb 15.2.10

Code:
procedure TFrmSesionUsuario.CerrarSesion(const AAppId: string);
var
  lApp: TIWApplication;
  lExpired, lLocked: Boolean;
  I: Integer;
  lForm: TIWAppForm;
begin
  lApp := gSessions.LookupAndLock(AAppId, lExpired, lLocked);
  if Assigned(lApp) and not lExpired then
  begin
    try
      for I := lApp.ActiveFormCount -1 downto 0 do
      begin       
          lForm := TIWAppForm(lApp.ActiveForms[I]);
          lForm.Release;
      end;
      lApp.SessionTimeOut := 1;
      lApp.Terminate;
    finally
      lApp.Unlock;
    end;
  end;
end;


The lApp variable is always NIL.

How can I overcome this problem?

TIA!
Reply
#2
There may be some info here that will help:

https://www.atozed.com/forums/thread-195...ml#pid6743

Dan
Reply
#3
(05-05-2021, 03:40 AM)DanBarclay Wrote: There may be some info here that will help:

  https://www.atozed.com/forums/thread-195...ml#pid6743

Dan

Hi DanBarclay.

Thanks for taking the time to respond.

But if you look carefully enough, that's the same code I used. The thing is that code only works if the AppId (SessionId) is in the same context of that UserSession or Application I would say.

What I'm trying to achieve is that with the AppId store in the database I could terminate ANY session of my applications from another application.

What I want to build is a session manager, so I could control per session how many users entered the system and then be able to release those sessions (terminate the applications) from the same manager.

Is that clear enough?
Reply
#4
Hi,

I'll test your code snippet. One thing that I can tell you that won't ever work is terminating the same session that is being used to execute that code.

Also, any other session currently locked (i.e. responding to any user requests) can't be terminated, until it can be locked.
Reply
#5
Hi, try this code:
Code:
procedure TFrmSesionUsuario.CerrarSesion(const AAppId: string);
var
  lApp: TIWApplication;
  I: Integer;
  xList: TList;
  foundit : boolean;
begin
  foundit := false;

  xList := GSessions.LockList(False);
  try
     for i := 0 to xList.Count - 1 do
     begin
        lApp := TIWApplication(xList[i]);
        if lApp.AppID = AAppId then
           begin
              foundit := true;
              break;
           end;
     end;
  finally
     GSessions.UnLockList(xList);
     if (foundit = true) then
        begin
           lApp.Terminate;
        end;
  end;
end;
Reply
#6
(05-06-2021, 11:43 AM)Jose Nilton Pace Wrote: Hi, try this code:
Code:
procedure TFrmSesionUsuario.CerrarSesion(const AAppId: string);
var
  lApp: TIWApplication;
  I: Integer;
  xList: TList;
  foundit : boolean;
begin
  foundit := false;

  xList := GSessions.LockList(False);
  try
     for i := 0 to xList.Count - 1 do
     begin
        lApp := TIWApplication(xList[i]);
        if lApp.AppID = AAppId then
           begin
              foundit := true;
              break;
           end;
     end;
  finally
     GSessions.UnLockList(xList);
     if (foundit = true) then
        begin
           lApp.Terminate;
        end;
  end;
end;

This code should not be used anymore (at least not in IW 15.1 and later). It simply won't complile.
Reply
#7
This code works correctly, as expected.
It terminates locks and terminates the session.

I believe that your problem is that you are trying to lock (and terminate) the same session that is running the command and that, by design, is not allowed. You can terminate all other sessions, except your own session, using this code.
To terminate your own session you must explicitly call IWApplication.Terminate, as usual.

Another point is that you don't need (nor should) release the application forms. The Session itself takes care of all forms when it is being released.

Code:

Code:
procedure TIWForm1.TerminateSession(const AAppId: string);
var
  lApp: TIWApplication;
  lExpired, lLocked: Boolean;
begin
  lApp := gSessions.LookupAndLock(AAppId, lExpired, lLocked);
  if Assigned(lApp) and not lExpired then
  begin
    try
      lApp.Terminate;
    finally
      lApp.Unlock;
    end;
  end;
end;
Reply
#8
(05-11-2021, 10:45 AM)Alexandre Machado Wrote: This code works correctly, as expected.
It terminates locks and terminates the session.

I believe that your problem is that you are trying to lock (and terminate) the same session that is running the command and that, by design, is not allowed. You can terminate all other sessions, except your own session, using this code.
To terminate your own session you must explicitly call IWApplication.Terminate, as usual.

Another point is that you don't need (nor should) release the application forms. The Session itself takes care of all forms when it is being released.

Code:

Code:
procedure TIWForm1.TerminateSession(const AAppId: string);
var
  lApp: TIWApplication;
  lExpired, lLocked: Boolean;
begin
  lApp := gSessions.LookupAndLock(AAppId, lExpired, lLocked);
  if Assigned(lApp) and not lExpired then
  begin
    try
      lApp.Terminate;
    finally
      lApp.Unlock;
    end;
  end;
end;

Hi!

I will try it and let you know the results. Thanks!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)