The following pseudocode may be a little too boiled down, but this is more of a general question
using (var thelock = await redLockFactory.CreateLockAsync())
{
await doWorkWhileLockIsOn();
}
How do I know the work done inside the async doWorkWhileLockIsOn()
method gets done before the lock goes out of scope?
Moreover, if the CreateLockAsync()
method is awaitable, how do I know I have a lock by the time I start running doWorkWhileLockIsOn()
?
If I Wait()
on these tasks, whats the point of using async at all?
I'm sure there is something simple I'm not getting because this seems so super basic. Is there some magic state machine stuff happening in the background, because running this sort of code in the debugger is showing me this won't work well.
Thanks for any advice.
To offer a little more background I was working to make some old TransactionScope
code become async and I got runtime errors about the TransactionScope
disposing on a different thread than the one it was created on. The structure of the code is almost identical to my redlock example above which is what got me worried.
How to dispose TransactionScope in cancelable async/await?
You can see there that there is a special constructor parameter and it seems to fix things. So I'm in the "why?" stage of thinking about all of this and wondering what happens with RedLock.