 .../CardManagement/CardAllocation/ChannelStates.cs |  108 +++++++++++---------
 1 file changed, 60 insertions(+), 48 deletions(-)

diff --git a/TvEngine3/TVLibrary/TvService/CardManagement/CardAllocation/ChannelStates.cs b/TvEngine3/TVLibrary/TvService/CardManagement/CardAllocation/ChannelStates.cs
index 20f0122..28c91d9 100644
--- a/TvEngine3/TVLibrary/TvService/CardManagement/CardAllocation/ChannelStates.cs
+++ b/TvEngine3/TVLibrary/TvService/CardManagement/CardAllocation/ChannelStates.cs
@@ -35,8 +35,11 @@ namespace TvService
     #region private members   
     
     private readonly object _lock = new object();
-    private readonly object _threadlock = new object();
-    private Thread _setChannelStatesThread;
+	 private readonly object _queuelock = new object();
+	 private readonly Queue<Action> workQueue = new Queue<Action>();
+	 private readonly AutoResetEvent wakeupEvent = new AutoResetEvent(false);
+
+	 private Thread _channelStatesThread;
 
     public ChannelStates(TvBusinessLayer businessLayer, TVController controller)
       : base(businessLayer, controller)
@@ -279,44 +282,59 @@ namespace TvService
       }
     }
 
+	 private void WorkThread(object waiter)
+	 {
+		 Action work;
+
+		 ((EventWaitHandle)waiter).Set();
+
+		 while (wakeupEvent.WaitOne(500)) {
+			 while (true) {
+				 lock (_queuelock) 
+					 work = workQueue.Count > 0 ? workQueue.Dequeue() : null;
+
+				 if (work == null)
+					 break;
+
+				 work();
+			 };
+		 }
+	 }
+
+
+	 private void DoThreadWork(Action work)
+	 {
+
+		 lock (_queuelock) {
+			 workQueue.Enqueue(work);
+			 wakeupEvent.Set();
+		 }
+
+		 if (_channelStatesThread == null || !_channelStatesThread.IsAlive) {
+			 _channelStatesThread = new Thread(WorkThread) { Name = "Channel state", IsBackground = true, Priority = ThreadPriority.BelowNormal };
+			 using (var waiter = new ManualResetEvent(false)) {
+				 _channelStatesThread.Start(waiter);
+				 waiter.WaitOne();
+			 }
+		 }
+	 }
+
+
     #endregion
 
     #region public members
 
-    private void AbortChannelStates()
-    {
-      lock (_threadlock)
-      {
-        if (_setChannelStatesThread != null && _setChannelStatesThread.IsAlive)
-        {
-          _setChannelStatesThread.Abort();
-        }
-      }
-    }
+	 public void SetChannelStates(IDictionary<int, ITvCardHandler> cards, ICollection<Channel> channels, IController tvController)
+	 {
+		 if (channels == null)
+			 return;
 
-    public void SetChannelStates(IDictionary<int, ITvCardHandler> cards, ICollection<Channel> channels,
-                                 IController tvController)
-    {
-      if (channels == null)
-      {
-        return;
-      }
-      AbortChannelStates();
-      //call the real work as a thread in order to avoid slower channel changes.
-      // find all users      
-      ICollection<IUser> allUsers = GetActiveUsers(cards);
-      ThreadStart starter = () => DoSetChannelStates(cards, channels, allUsers, tvController);
-      lock (_threadlock)
-      {
-        _setChannelStatesThread = new Thread(starter)
-                                    {
-                                      Name = "Channel state thread",
-                                      IsBackground = true,
-                                      Priority = ThreadPriority.Lowest
-                                    };
-        _setChannelStatesThread.Start();
-      }
-    }    
+		 // find all users      
+		 var allUsers = GetActiveUsers(cards);
+
+		 //do the real work in thread in order to avoid slower channel changes.
+		 DoThreadWork(new Action(() => DoSetChannelStates(cards, channels, allUsers, tvController)));
+	 }
 
     /// <summary>
     /// Gets a list of all channel states    
@@ -326,21 +344,15 @@ namespace TvService
                                                           ref IUser user,
                                                           IController tvController)
     {
-      if (channels == null)
-      {
-        return null;
-      }
+		 if (channels == null)
+			 return null;
 
-      var allUsers = new List<IUser>();
-      allUsers.Add(user);
+		 var allUsers = new List<IUser>();
+		 if (user != null)
+			 allUsers.Add(user);
 
-      DoSetChannelStates(cards, channels, allUsers, tvController);
-
-      if (allUsers.Count > 0)
-      {
-        return allUsers[0].ChannelStates;
-      }
-      return new Dictionary<int, ChannelState>();
+		 DoSetChannelStates(cards, channels, allUsers, tvController);
+		 return allUsers.Count > 0 ? allUsers[0].ChannelStates : new Dictionary<int, ChannelState>();
     }
 
     #endregion
