Is obtaining a lock on the UI thread a bad practice? For example I have a cache object which is being modified by both background threads and the UI thread. Any modifications to the cache is wrapped inside a lock. The pseudo code is as follows:
public class Cache
{
private readonly Dictionary<string, IEnumerable<string>> _cache;
private readonly object _onCache = new object();
public Cache()
{
_cache = new Dictionary<string, IEnumerable<string>>();
}
//Called by worker threads
public void RefreshInBackground(IEnumerable<string> items, string key)
{
lock (_onCache)
{
_cache[key] = items;
}
}
//Called by UI thread.Does this lead to any issues since UI thread is waiting on a lock?
public void RefreshInForeground(IEnumerable<string> items, string key)
{
lock (_onCache)
{
_cache[key] = items;
}
}
}
When the RefreshInForegound is called by the UI thread it has to wait on the lock (maybe), and the question is, is this a bad practice and if yes why?
Thanks, -Mike