Obtaining lock on a UI thread
<p>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:</p>

<pre><code> public class Cache
    {
        private readonly Dictionary&lt;string, IEnumerable&lt;string&gt;&gt; _cache;
        private readonly object _onCache = new object();
        public Cache()
        {
            _cache = new Dictionary&lt;string, IEnumerable&lt;string&gt;&gt;();
        }

        //Called by worker threads
        public void RefreshInBackground(IEnumerable&lt;string&gt; 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&lt;string&gt; items, string key)
        {
            lock (_onCache)
            {
                _cache[key] = items;
            }
        }
    }
</code></pre>

<p>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?</p>

<p>Thanks,
-Mike</p>
