C
C#15mo ago
SWEETPONY

✅ Is it possible to lock method by id?

I don't know how to write correct question but I would try to describe my idea. Let's say I have this method: public async Task<Dto> Modify(string groupId) => .. Imagine there are 4 people inside 1 group and someone called Modify method. Modification was started, it takes 1 minute for example and someone from this group called Modify method again. I want to deny the second user to use Modify method because first user didn't get the response. How can I do it?
19 Replies
DaVinki
DaVinki15mo ago
You can use the TryEnter method on a monitor. If it failed, another thread is already entered
SWEETPONY
SWEETPONYOP15mo ago
Yes, I know about lock but actually I don't need it. Imagine we have 2 groups with people so lock can't help me
jen
jen15mo ago
if someone is already modifying, do you want the second caller to wait, or fail? you can have a lock for each group
SWEETPONY
SWEETPONYOP15mo ago
named semaphore? by the way I think lock and async things are not good things idk
jen
jen15mo ago
i would use a ConcurrentDictionary<groupId, object> correct
SWEETPONY
SWEETPONYOP15mo ago
hm didn't think about it maybe wait..
jen
jen15mo ago
the async method can change threads, which could lead to problems with the lock i dont know about that, youll have to do some research
DaVinki
DaVinki15mo ago
Are you sure sharing something between multiple groups and locking only within those groups is the ideal scenario? What if you made an object for each group that they own? It might be an XY problem
Joschi
Joschi15mo ago
What about an AsyncLocal<bool> in the group object that keeps track of the state?
cap5lut
cap5lut15mo ago
for lock behaviour, as in waiting til u acquire the lock, i usually use a SemaphoreSlim if i want to try to aquire a lock but otherwise fail, i usually use an int and Interlocked.Exchange() note that these ways of doing things arent safe for reentrant
Baturhan
Baturhan15mo ago
Use redis for distrubted lock your keyword is "Distributed Locks"
jen
jen15mo ago
i dont think thats needed from a quick google, you should be fine with a SemaphoreSlim.WaitAsync
cap5lut
cap5lut15mo ago
u could also choose to use dotNext.Threading, it comes with a bunch of async friendly locks AsyncExclusiveLock (https://dotnet.github.io/dotNext/features/threading/exclusive.html) is just one among many
SWEETPONY
SWEETPONYOP15mo ago
it will be fun if we can do this: using @lock = lock;
jen
jen15mo ago
would be cool but its not a good idea to abuse Dispose like that
SWEETPONY
SWEETPONYOP15mo ago
hehe
lock ("12")
{

}

namespace System.Threading
{
public static class Monitor
{
public static void Enter(
object obj,
ref bool taken)
{
Console.WriteLine("Hello, world!");
taken = true;
}

public static void Exit(object obj)
{
Console.WriteLine("Bye, world!");
}
}
};
lock ("12")
{

}

namespace System.Threading
{
public static class Monitor
{
public static void Enter(
object obj,
ref bool taken)
{
Console.WriteLine("Hello, world!");
taken = true;
}

public static void Exit(object obj)
{
Console.WriteLine("Bye, world!");
}
}
};
now Enter and Exit should implement AcquireAsync from AsyncExclusiveLock harold
cap5lut
cap5lut15mo ago
using + IDisposable is quite often used for such stuff, especially u see often some kind of lease struct for some array which simply returns the array to the pool
jen
jen15mo ago
it makes sense for something that acquires and releases yeah
SWEETPONY
SWEETPONYOP15mo ago
thanks for helping

Did you find this page helpful?