i just wrote a code, and then i found out that there are some issue with the monitor.wait, forcing me to do the operation within the locks, i wanted to now if this is a good way to keep a thread waiting,....
i'm not sure if the thread.join would do the job, as there are lots of threads running within my application, and each do specific job, that they may terminate within the time...
here is my code:
public static class TaskManager
{
private static readonly object UpdateLock = new object();
private static readonly object WaitLock = new object();
private static readonly LiaisonDb _db = new LiaisonDb();
private static List<liaQueue> _liaQueueList = new List<liaQueue>();
private static DateTime _lastUpdate = new DateTime();
public static liaQueue GetTask(string sessionType)
{
liaQueue task;
lock (UpdateLock)
{
if (_lastUpdate < DateTime.Now.AddSeconds(-5))
{
Thread t = new Thread(UpdateCache) {IsBackground = true};
t.Start();
lock (WaitLock)
{
Monitor.Wait(WaitLock);
}
_lastUpdate = DateTime.Now;
}
task = _liaQueueList
.FirstOrDefault(w => w.Stat == 0
&& w.Type != null
|| string.Equals(w.Type, sessionType));
}
return task;
}
private static void UpdateCache()
{
try
{
_liaQueueList = _db.liaQueue.Where(w => w.Stat == 0).ToList();
}
finally
{
lock (WaitLock)
{
Monitor.Pulse(WaitLock);
}
}
}
}
As you see i put two lock, and one of them is only for monitor.wait, keep the thread waiting for the answer...
i think i also have to returns null while the cache is getting refreshed?...