 .../Implementations/DVB/Graphs/TvDvbChannel.cs     |  139 ++++++++++---------
 1 files changed, 73 insertions(+), 66 deletions(-)

diff --git a/TvEngine3/TVLibrary/TVLibrary/Implementations/DVB/Graphs/TvDvbChannel.cs b/TvEngine3/TVLibrary/TVLibrary/Implementations/DVB/Graphs/TvDvbChannel.cs
index 37f01b5..d40ae4d 100644
--- a/TvEngine3/TVLibrary/TVLibrary/Implementations/DVB/Graphs/TvDvbChannel.cs
+++ b/TvEngine3/TVLibrary/TVLibrary/Implementations/DVB/Graphs/TvDvbChannel.cs
@@ -287,91 +287,98 @@ namespace TvLibrary.Implementations.DVB
         // allow retry to look for PMT in PAT if original one times out
         while (++retryCount <= 2)
         {
-          if (SetupPmtGrabber(lookForPid, channel.ServiceId)) // pat lookup by sid or PMT pid
+          try
           {
-            DateTime dtNow = DateTime.Now;
-            int timeoutPMT = _parameters.TimeOutPMT * 1000;
-            if (alwaysUsePATLookup)
-            {
-              Log.Log.Debug("WaitForPMT: Using new way for PMT grabbing via PAT");
-              Log.Log.Debug("WaitForPMT: Waiting for SID {0}", channel.ServiceId);
-            }
-            else
+            if (SetupPmtGrabber(lookForPid, channel.ServiceId)) // pat lookup by sid or PMT pid
             {
-              Log.Log.Debug("WaitForPMT: Waiting for PMT {0:X}", _pmtPid);
-            }
+              DateTime dtNow = DateTime.Now;
+              int timeoutPMT = _parameters.TimeOutPMT * 1000;
+              if (alwaysUsePATLookup)
+              {
+                Log.Log.Debug("WaitForPMT: Using new way for PMT grabbing via PAT");
+                Log.Log.Debug("WaitForPMT: Waiting for SID {0}", channel.ServiceId);
+              }
+              else
+              {
+                Log.Log.Debug("WaitForPMT: Waiting for PMT {0:X}", _pmtPid);
+              }
 
-            if (_eventPMT.WaitOne(timeoutPMT, true))
-            {
-              TimeSpan ts = DateTime.Now - dtNow;
-              Log.Log.Debug("WaitForPMT: Found PMT after {0} seconds.", ts.TotalSeconds);
-              DateTime dtNowPMT2CAM = DateTime.Now;
-              bool sendPmtToCamDone = false;
-              try
+              if (_eventPMT.WaitOne(timeoutPMT, true))
               {
-                while (ts.TotalMilliseconds < timeoutPMT && !sendPmtToCamDone)
-                  //lets keep trying to send pmt2cam and at the same time obey the timelimit specified in timeoutPMT
+                TimeSpan ts = DateTime.Now - dtNow;
+                Log.Log.Debug("WaitForPMT: Found PMT after {0} seconds.", ts.TotalSeconds);
+                DateTime dtNowPMT2CAM = DateTime.Now;
+                bool sendPmtToCamDone = false;
+                try
                 {
-                  ts = DateTime.Now - dtNow;
-                  bool updatePids;
-                  int waitInterval; //ms         
-                  sendPmtToCamDone = SendPmtToCam(out updatePids, out waitInterval);
-                  if (sendPmtToCamDone)
+                  while (ts.TotalMilliseconds < timeoutPMT && !sendPmtToCamDone)
+                  //lets keep trying to send pmt2cam and at the same time obey the timelimit specified in timeoutPMT
                   {
-                    if (updatePids)
+                    ts = DateTime.Now - dtNow;
+                    bool updatePids;
+                    int waitInterval; //ms         
+                    sendPmtToCamDone = SendPmtToCam(out updatePids, out waitInterval);
+                    if (sendPmtToCamDone)
                     {
-                      if (_channelInfo != null)
+                      if (updatePids)
                       {
-                        SetMpegPidMapping(_channelInfo);
-                        if (_mdplugs != null && _channelInfo.scrambled)
+                        if (_channelInfo != null)
                         {
-                          _mdplugs.SetChannel(_currentChannel, _channelInfo, false);
+                          SetMpegPidMapping(_channelInfo);
+                          if (_mdplugs != null && _channelInfo.scrambled)
+                          {
+                            _mdplugs.SetChannel(_currentChannel, _channelInfo, false);
+                          }
+                        }
+                        Log.Log.Info("subch:{0} stop tif", _subChannelId);
+                        if (_filterTIF != null)
+                        {
+                          _filterTIF.Stop();
                         }
-                      }
-                      Log.Log.Info("subch:{0} stop tif", _subChannelId);
-                      if (_filterTIF != null)
-                      {
-                        _filterTIF.Stop();
                       }
                     }
-                  }
-                  else
-                  {
-                    Log.Log.Debug("WaitForPMT: waiting for SendPmtToCam {0} ms.", ts.TotalMilliseconds);
-                    Thread.Sleep(waitInterval);
+                    else
+                    {
+                      Log.Log.Debug("WaitForPMT: waiting for SendPmtToCam {0} ms.", ts.TotalMilliseconds);
+                      Thread.Sleep(waitInterval);
+                    }
                   }
                 }
-              }
-              catch (Exception ex)
-              {
-                Log.Log.WriteFile("subch:{0}", ex.Message);
-                Log.Log.WriteFile("subch:{0}", ex.Source);
-                Log.Log.WriteFile("subch::{0}", ex.StackTrace);
-              }
-              TimeSpan tsPMT2CAM = DateTime.Now - dtNowPMT2CAM;
-              _listenCA = false;
-              if (!sendPmtToCamDone)
-              {
-                Log.Log.Debug("WaitForPMT: Timed out sending PMT to CAM {0} seconds.", tsPMT2CAM.TotalSeconds);
+                catch (Exception ex)
+                {
+                  Log.Log.WriteFile("subch:{0}", ex.Message);
+                  Log.Log.WriteFile("subch:{0}", ex.Source);
+                  Log.Log.WriteFile("subch::{0}", ex.StackTrace);
+                }
+                TimeSpan tsPMT2CAM = DateTime.Now - dtNowPMT2CAM;
+                _listenCA = false;
+                if (!sendPmtToCamDone)
+                {
+                  Log.Log.Debug("WaitForPMT: Timed out sending PMT to CAM {0} seconds.", tsPMT2CAM.TotalSeconds);
+                }
+                else
+                {
+                  Log.Log.Debug("WaitForPmt: PMT handling took {0} seconds.", tsPMT2CAM.TotalSeconds);
+                }
+                // PMT was found so exit here
+                foundPMT = true;
+                break;
               }
               else
               {
-                Log.Log.Debug("WaitForPmt: PMT handling took {0} seconds.", tsPMT2CAM.TotalSeconds);
+                // Timeout waiting for PMT
+                TimeSpan ts = DateTime.Now - dtNow;
+                Log.Log.Debug("WaitForPMT: Timed out waiting for PMT after {0} seconds. Increase the PMT timeout value?",
+                              ts.TotalSeconds);
+                Log.Log.Debug("Setting to 0 to search for new PMT.");
+                lookForPid = 0;
               }
-              // PMT was found so exit here
-              foundPMT = true;
-              break;
-            }
-            else
-            {
-              // Timeout waiting for PMT
-              TimeSpan ts = DateTime.Now - dtNow;
-              Log.Log.Debug("WaitForPMT: Timed out waiting for PMT after {0} seconds. Increase the PMT timeout value?",
-                            ts.TotalSeconds);
-              Log.Log.Debug("Setting to 0 to search for new PMT.");
-              lookForPid = 0;
             }
           }
+          finally
+          {
+            _pmtRequested = false; // reset PMT request flag, it means that we have finished processing of PMT
+          }
         } // retry loop
       }
 
@@ -1365,7 +1372,7 @@ namespace TvLibrary.Implementations.DVB
         }
       }
       PersistPMTtoDataBase(pmtPid);
-      _pmtRequested = false; // once received, reset
+      //_pmtRequested = false; // once received, reset
       return 0;
     }
 
