Delphi-PRAXiS
Seite 2 von 3     12 3      

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)

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)


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:50 Uhr.
Seite 2 von 3     12 3      

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