Claudiu HBann
Claudiu HBann
CC#
Created by Claudiu HBann on 7/31/2024 in #help
CSharp's C++ std::variant
Is there a std::variant in C# like in C++? If not what is the closest thing to it?
11 replies
CC#
Created by Claudiu HBann on 2/17/2024 in #help
async/await and mutexes
I have a service that i will inject it as singleton that has:
c#
private readonly Dictionary<string, List<string>> _fromToAll = [];
private readonly Mutex _mutex = new();
c#
private readonly Dictionary<string, List<string>> _fromToAll = [];
private readonly Mutex _mutex = new();
and I want the dictionary to be lazy initialized only once when needed like:
c#
private async Task<Dictionary<string, List<string>>> FindFromToAll()
{
try
{
_mutex.WaitOne();
if (_fromToAll.Count > 0)
{
return _fromToAll;
}

foreach (var serviceType in FindAllServiceTypes())
{
var service = ActivatorUtilities.CreateInstance(provider, serviceType);
var fromTo = await service.Invoke<Task<List<string>>>("FromTo");
_fromToAll.Add(serviceType.Name.Replace("Service", null), fromTo);
}

return _fromToAll;
}
finally
{
_mutex.ReleaseMutex();
}
}
c#
private async Task<Dictionary<string, List<string>>> FindFromToAll()
{
try
{
_mutex.WaitOne();
if (_fromToAll.Count > 0)
{
return _fromToAll;
}

foreach (var serviceType in FindAllServiceTypes())
{
var service = ActivatorUtilities.CreateInstance(provider, serviceType);
var fromTo = await service.Invoke<Task<List<string>>>("FromTo");
_fromToAll.Add(serviceType.Name.Replace("Service", null), fromTo);
}

return _fromToAll;
}
finally
{
_mutex.ReleaseMutex();
}
}
but when I invoke and wait for the FromTo Task that returns a list of strings the context changes and when i release the mutex it crashes because i release it from an another thread, the exception message looks like this: "Object synchronization method was called from an unsynchronized block of code." Even if i add ConfigureAwait(true) doesn't switch the context to the captured one. Any idea why this happens or how can I do it differently? Thank you in advance 😃
9 replies
CC#
Created by Claudiu HBann on 12/25/2023 in #help
C# Tasks question
hello, idk if this is the right place to ask but i have a question about how the tasks in c# work, like, let's say we have an application with an GUI and on an event on a button click I await a task that it will take about 2-3 seconds, but the GUI doesnt freeze and it works just fine, the question is how, because I am wating for that task to finish on the UI thread and it should freeze. As an example I have some experience with C++/WinRT WinUI3 and for something like this to work you need to switch the context with winrt::resume_background() and now you are on a thread from a thread pool, you do your job and next you switch back on the UI thread with winrt::resume_foreground(context), the context must be the one captured before resuming to the background or use the UI dispatcher to run something on the UI thread There is a list of tasks and my task goes there and somewhere in time it runs on the UI thread too? the task contains multiple small tasks and are each of them go in the "list" one by one?
23 replies