r/xamarindevelopers • u/RenSanders • Jun 25 '21
How did I survive all this while without knowing C# Semaphores?
Just learned about SemaphoreSlim today, and boy... this makes my code so clean, especially the popups and message prompts.
Can't believe how I survived all this while without knowing this!!
Anybody on the same boat as me?
1
u/gybemeister Jun 25 '21
What do you use it for in the Xamarin context?
0
u/RenSanders Jun 25 '21
For state management offcourse?? Isn't thats what it's for
var result = await SOMEPPOPUP(string[] selections)
4
u/antoine2142 Jun 25 '21
There is no semaphore in your example. It's just awaiting the result from the popup.
1
u/RenSanders Jun 26 '21
The 'SOMEPOPUP' popup is a custom (Xaml). Rather than having weird state management capturing taps everywhere; this provides a nice 'Awaitable' flow control.
How else would one implement this if not via Semaphores?
2
u/antoine2142 Jun 26 '21
I truly cannot see why a popup would require controlling the number of threads that can access a certain section of code or variable, which is usually what semaphores are used for in user applications. Maybe I can learn something new today if you have a source where I can see how exactly semaphores are used in this context
→ More replies (1)3
u/gybemeister Jun 25 '21
Sorry but that does not make any sense to me. I found a couple of articles about the use of SemaphoreSlim to throttle processing, for example, when you create 50 tasks but want to process 10 at a time, then you can use a SemaphoreSlim for that. I could not find anything about SemaphoreSlim and Xamarin or even UI.
2
u/RenSanders Jun 26 '21 edited Jun 26 '21
This should be as simple as this=> Semaphores make a nice procedural flow for complex states like in app wizards and such
public async Task<SelectorResult<string>> ShowSelectorPopup(IEnumerable<string> selections)
{
Semaphore = new SemaphoreSlim(0);
this.Show = true; //Shows the popup
await Semaphore.WaitAsync(); //will be released after user taps 'OK'
this.Show = false; //Hide popup
return new SelectorResult<string>(this.Selected);
}
Inside Selector.Xaml, the semaphore is released when a user taps 'ok'. This ensure Popup.Xaml is totally decoupled and only interacts with the application though a single 'ShowSelectorPopup' line
3
u/valdetero Jun 26 '21
Why not use a TaskCompletionSource? That’s what I did for waiting for pop ups.
1
1
2
u/jim-dog-x Jun 25 '21
As others have mentioned, be care of deadlocks.
I use a SemaphoreSlim in only one place in our app. We created a custom control that runs on a timer. It uses SkiaSharp to draw a "countdown timer" with a circle that slowly disappears.
What we found is that every blue moon the countdown would get to 0, fire the callback, the view would go away and there was one last OnTimer() in the queue that would try to draw on the Canvas... boom, crash.
This one bug was causing our Android version of the app to have a 3 - 4% crash rate. Adding the semaphore, making sure no more paint calls were being attempted before closing the view and now our crash rate is ~0.3%. There may be a better way of handling this, but this worked for us.
So they can be very helpful, in the right situation.
2
8
u/r2d2rigo Jun 25 '21
You shouldn't be using them unless you need multithreaded synchronization. Use async methods instead or you will end with a deadlock/frozen UI pretty easily.