简体   繁体   中英

How to limit number of users and/or threads for a .net library?

How can we limit the number of users and/or threads for a .net library?

We want to have different library editions for different user groups: 1) Single user: Max 1 user with 1 thread

2) Multiple user with max 50 users and/or threads As an example, this edition can accommodate 50 users can have each 1 thread, or 25 users with each 2 threads

3) Multiple user with max 100 users and/or threads .. and so on.

How can we do this the easiest way with c#? Selina

Define a sempahore and synchronize on it:

static readonly SemaphoreSlim semaphore = new Semaphore(50);

...

//before your library does anything
semaphore.Wait(TimeSpan.FromSeconds(120)); //timeout to prevent deadlocks

Fortunately, everything that you need is built into this class.

You should probably add a wait timeout or you risk causing deadlocks and your customers will deem your library unreliable. If the wait times out you should probably just allow the access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM