Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   limiting number of instances in Windows terminal server (https://www.delphipraxis.net/200895-limiting-number-instances-windows-terminal-server.html)

imadam 4. Jun 2019 20:46

limiting number of instances in Windows terminal server
 
Hello,
I can read a little of German, but to write, not possible. So here is my question in English:
I have Delphi app which need to execute in Windows Terminal Server (nowdays RDS). We would like to be able to control how many instances user can run in their session. So for example:
- user 1 can run two instances of app 1
- user 2 can run 1 instance of app 1

We are able to fix this on Windows to have only one instance of application running but on TS is a bit problem for us. Anybody had similar problem?

Thank you.

Schokohase 4. Jun 2019 22:29

AW: limiting number of instances in Windows terminal server
 
Do you use a mutex?

If yes, a global or a session mutex?

API 4. Jun 2019 23:28

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von imadam (Beitrag 1433893)
Hello,
I can read a little of German, but to write, not possible.

https://en.delphipraxis.net/

imadam 5. Jun 2019 06:02

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von Schokohase (Beitrag 1433907)
Do you use a mutex?

If yes, a global or a session mutex?

This is new app. Starting from scratch. So Delphi best practice should be applied here :-).

imadam 5. Jun 2019 06:04

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von API (Beitrag 1433910)
Zitat:

Zitat von imadam (Beitrag 1433893)
Hello,
I can read a little of German, but to write, not possible.

https://en.delphipraxis.net/

Thank you. Looks like same username doesn't work on EN site.

TBx 5. Jun 2019 06:11

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von imadam (Beitrag 1433916)
[Looks like same username doesn't work on EN site.

en.delphipraxis.net has not the same user database. You have to clone your account via this Link.

imadam 5. Jun 2019 06:14

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von TBx (Beitrag 1433918)
Zitat:

Zitat von imadam (Beitrag 1433916)
[Looks like same username doesn't work on EN site.

en.delphipraxis.net has not the same user database. You have to clone your account via this Link.

Tnx Tomas. Will try to clone.

imadam 5. Jun 2019 06:25

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von imadam (Beitrag 1433919)
Zitat:

Zitat von TBx (Beitrag 1433918)
Zitat:

Zitat von imadam (Beitrag 1433916)
[Looks like same username doesn't work on EN site.

en.delphipraxis.net has not the same user database. You have to clone your account via this Link.

Tnx Tomas. Will try to clone.

Cloned but doesn't work.

Schokohase 5. Jun 2019 07:29

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von imadam (Beitrag 1433914)
Zitat:

Zitat von Schokohase (Beitrag 1433907)
Do you use a mutex?

If yes, a global or a session mutex?

This is new app. Starting from scratch. So Delphi best practice should be applied here :-).

You already have a working solution for
Zitat:

Zitat von imadam (Beitrag 1433893)
We are able to fix this on Windows to have only one instance of application running

so please show us it and we can tell yo how to change it for running on TS

imadam 5. Jun 2019 07:35

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von Schokohase (Beitrag 1433923)
Zitat:

Zitat von imadam (Beitrag 1433914)
Zitat:

Zitat von Schokohase (Beitrag 1433907)
Do you use a mutex?

If yes, a global or a session mutex?

This is new app. Starting from scratch. So Delphi best practice should be applied here :-).

You already have a working solution for
Zitat:

Zitat von imadam (Beitrag 1433893)
We are able to fix this on Windows to have only one instance of application running

so please show us it and we can tell yo how to change it for running on TS

I read my post again. It is bit confusing :-(. So we started with prototype. Playing around. We were able limit this to one instance of the app on Windows Desktop. However, we would like to be able to set number of instances (like 3 for user 1, 1 for user 2) in Terminal server. Also, if is there some best practice for Delphi developing this kind of apps, it would be great if somebody can share it. Let me know what to share ...

Schokohase 5. Jun 2019 08:10

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von imadam (Beitrag 1433924)
We were able limit this to one instance of the app on Windows Desktop.

I guess you use a mutex for that.

For Terminal Server (or even in generell) do not use a global mutex as this would be for the whole system.

For an application with one instance you need one mutex.
For an application with two instances you need two mutexes.
...

Klaus01 5. Jun 2019 08:14

AW: limiting number of instances in Windows terminal server
 
.. but whith muutexes you have to decide how many instances can be started - at design time.
These settings are then based on application level - not on user level.

best regards
Klaus

mjustin 5. Jun 2019 08:20

AW: limiting number of instances in Windows terminal server
 
Did you try TJvAppInstances already? It is designed for limiting app instances, and it allows to set the maximum number of simultaneous instances using the property MaxInstances. I am not soure however if it is also suited for a TS.

Dalai 5. Jun 2019 08:22

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von Klaus01 (Beitrag 1433926)
.. but whith muutexes you have to decide how many instances can be started - at design time.

Uh, no. A Mutex is based on a name, a string, and strings can be built at runtime. Example from one of my projects where the mutex name is based on a concatenation of several strings:
Delphi-Quellcode:
hMutex:= CreateMutex(nil, True, PRODUCTNAME + PRODUCTNAME + PRODUCTCOPYRIGHT);
Using usernames and an appended counter instead is trivial. Reading the information that user A can start the application X times and user B can start it only once is trivial, too (Registry, INI, XML, whatever). Use that in conjunction with a for-loop that tries to create mutexes until either all of them already exist, or the maximum allowed instances are reached, and you have reached your goal.

Regards
Dalai

imadam 5. Jun 2019 08:28

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von Dalai (Beitrag 1433930)
Zitat:

Zitat von Klaus01 (Beitrag 1433926)
.. but whith muutexes you have to decide how many instances can be started - at design time.

Uh, no. A Mutex is based on a name, a string, and strings can be built at runtime. Example from one of my projects where the mutex name is based on a concatenation of several strings:
Delphi-Quellcode:
hMutex:= CreateMutex(nil, True, PRODUCTNAME + PRODUCTNAME + PRODUCTCOPYRIGHT);
Using usernames and an appended counter instead is trivial. Reading the information that user A can start the application X times and user B can start it only once is trivial, too (Registry, INI, XML, whatever). Use that in conjunction with a for-loop that tries to create mutexes until either all of them already exist, or the maximum allowed instances are reached, and you have reached your goal.

Regards
Dalai

So far, my research gave me:
- I have to list processes in TS session
- I have to get username from TS enviroment which is logged in
and simple compare number of instances in question with number of those processes already running.
Is this wrong approach ?
Yet to figure out how to read logged user and processes over Win32 API :-(.

Dalai 5. Jun 2019 08:40

AW: limiting number of instances in Windows terminal server
 
Yes, reading the number of processes running as user X is another possible approach. Unfortunately I don't know how to read the user a process runs as, even less when TS is involved.

Regards
Dalai

hoika 5. Jun 2019 08:52

AW: limiting number of instances in Windows terminal server
 
Hello,
use a GUID as (global) Mutex-Name with _1, _2 and so on.

if _1 is used, use _2.
if _x is used, you have your maximum instances ...

quick&dirty ;)


or just said: use JEDI JvAppInst.pas
hey use some form of hidden window for each instance

mjustin 5. Jun 2019 09:00

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von Dalai (Beitrag 1433932)
Yes, reading the number of processes running as user X is another possible approach. Unfortunately I don't know how to read the user a process runs as, even less when TS is involved.

It is easy to count the instances running in the current session. Example in .Net:

Code:
private static int CountApplicationInstances()
    {
        var currentProcess = Process.GetCurrentProcess();
        var processes = Process.GetProcessesByName(currentProcess.ProcessName);

        // test if there's another process running in current session.
        var intTotalRunningInCurrentSession = processes.Count(prc => prc.SessionId == currentProcess.SessionId);

        return intTotalRunningInCurrentSession;
    }
(based on https://stackoverflow.com/a/36210660/80901)

imadam 5. Jun 2019 09:13

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von mjustin (Beitrag 1433929)
Did you try TJvAppInstances already? It is designed for limiting app instances, and it allows to set the maximum number of simultaneous instances using the property MaxInstances. I am not soure however if it is also suited for a TS.

We tested this: http://www.delphidabbler.com/articles?article=13&part=4 and this works on Windows 10 limiting to 1 instance. But it doesn't work on TS. It actually on TS allows only one user to run only once instance. Whom ever get first to it :-(

mjustin 5. Jun 2019 09:20

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von imadam (Beitrag 1433935)
Zitat:

Zitat von mjustin (Beitrag 1433929)
Did you try TJvAppInstances already? It is designed for limiting app instances, and it allows to set the maximum number of simultaneous instances using the property MaxInstances. I am not soure however if it is also suited for a TS.

We tested this: http://www.delphidabbler.com/articles?article=13&part=4 and this works on Windows 10 limiting to 1 instance. But it doesn't work on TS. It actually on TS allows only one user to run only once instance. Whom ever get first to it :-(

Ok, the code on (http://www.delphidabbler.com/articles?article=13) does not use the JVCL TJvAppInstances, but good to know ;)

I also posted another suggestion (see #18)

imadam 5. Jun 2019 09:24

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von mjustin (Beitrag 1433929)
Did you try TJvAppInstances already? It is designed for limiting app instances, and it allows to set the maximum number of simultaneous instances using the property MaxInstances. I am not soure however if it is also suited for a TS.

No, I haven't tested it. Will review. But on initial aim, this won't help.

Schokohase 5. Jun 2019 09:28

AW: limiting number of instances in Windows terminal server
 
Delphi-Quellcode:
TJvAppInstances
will limit the applications inside the SESSION.

So => yes, this will work on TS too.

But, a single user can open more than one session on a TS (you can limit it optional to only one session per user).

imadam 5. Jun 2019 09:30

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von Schokohase (Beitrag 1433938)
Delphi-Quellcode:
TJvAppInstances
will limit the applications inside the SESSION.

So => yes, this will work on TS too.

But, a single user can open more than one session on a TS (you can limit it optional to only one session per user).

Have you tried it already?

peterbelow 5. Jun 2019 10:40

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von imadam (Beitrag 1433893)
Hello,
I can read a little of German, but to write, not possible. So here is my question in English:
I have Delphi app which need to execute in Windows Terminal Server (nowdays RDS). We would like to be able to control how many instances user can run in their session. So for example:
- user 1 can run two instances of app 1
- user 2 can run 1 instance of app 1

We are able to fix this on Windows to have only one instance of application running but on TS is a bit problem for us. Anybody had similar problem?

Thank you.

This is the classical scenario for a licence server. You have a separate process (Windows or web service) running on a known platform. This process accesses a repository of licenses assigned to specific users. This can be a simple file. When your program is started it identifies the current user (Windows GetUsername API function) and then asks the licence server whether there is a free licence for that user available. If not it exits with a suitably ascerbic error message. If a licence is available it claims it from the server, usually giving the server a combination of computer name or ip address and process ID as a means to identify the claimer of the licence for the licence admin. When the application ends it returns the licence to the server.

The main problem in this scenario is how to deal with unexpected termination of the program holding the licence. In this case the licence is not properly returned to the server for later reuse. How to handle that depends on specifics of the environment. You can, for example, implement a mechanism that allows the server to check at intervals whether the processes having a licence in use are still running. Or you can require the program to regularly "renew" the claim they have on a licence, e.g. at 10 minute intervals. The licence is then reset to available if two of these intervals have passed without a renewal of the licence.

Schokohase 5. Jun 2019 11:14

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von imadam (Beitrag 1433939)
Zitat:

Zitat von Schokohase (Beitrag 1433938)
Delphi-Quellcode:
TJvAppInstances
will limit the applications inside the SESSION.

So => yes, this will work on TS too.

But, a single user can open more than one session on a TS (you can limit it optional to only one session per user).

Have you tried it already?

I do not need to try - I saw the source code and I saw how it is implemented and working

imadam 5. Jun 2019 11:30

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von Schokohase (Beitrag 1433946)
Zitat:

Zitat von imadam (Beitrag 1433939)
Zitat:

Zitat von Schokohase (Beitrag 1433938)
Delphi-Quellcode:
TJvAppInstances
will limit the applications inside the SESSION.

So => yes, this will work on TS too.

But, a single user can open more than one session on a TS (you can limit it optional to only one session per user).

Have you tried it already?

I do not need to try - I saw the source code and I saw how it is implemented and working

Ok :-). Thank you.

imadam 5. Jun 2019 11:59

AW: limiting number of instances in Windows terminal server
 
Zitat:

Zitat von peterbelow (Beitrag 1433944)
Zitat:

Zitat von imadam (Beitrag 1433893)
Hello,
I can read a little of German, but to write, not possible. So here is my question in English:
I have Delphi app which need to execute in Windows Terminal Server (nowdays RDS). We would like to be able to control how many instances user can run in their session. So for example:
- user 1 can run two instances of app 1
- user 2 can run 1 instance of app 1

We are able to fix this on Windows to have only one instance of application running but on TS is a bit problem for us. Anybody had similar problem?

Thank you.

This is the classical scenario for a licence server. You have a separate process (Windows or web service) running on a known platform. This process accesses a repository of licenses assigned to specific users. This can be a simple file. When your program is started it identifies the current user (Windows GetUsername API function) and then asks the licence server whether there is a free licence for that user available. If not it exits with a suitably ascerbic error message. If a licence is available it claims it from the server, usually giving the server a combination of computer name or ip address and process ID as a means to identify the claimer of the licence for the licence admin. When the application ends it returns the licence to the server.

The main problem in this scenario is how to deal with unexpected termination of the program holding the licence. In this case the licence is not properly returned to the server for later reuse. How to handle that depends on specifics of the environment. You can, for example, implement a mechanism that allows the server to check at intervals whether the processes having a licence in use are still running. Or you can require the program to regularly "renew" the claim they have on a licence, e.g. at 10 minute intervals. The licence is then reset to available if two of these intervals have passed without a renewal of the licence.

You can look at it from that point. And it is good point. However, here main reason is to prevent users hogging RDS server since they open multiple application to use (it is easier for them) and if you have 100 users opening up same up 3-4 times, then it is like 400 users on server which should support 100 users :-)


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:18 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz