private void RunNextInQueue()
 {
     if (pendingCreativeSets.Any())
     {
         IEnumerable <Creative> creativesToMerge;
         if (runningCreativeSet != null && runningCreativeSet.RunningCreatives != null)
         {
             creativesToMerge = runningCreativeSet.RunningCreatives;
             runningCreativeSet.RunningCreatives = null;
         }
         else
         {
             creativesToMerge = Enumerable.Empty <Creative>();
         }
         runningCreativeSet = pendingCreativeSets.Dequeue();
         runningCreativeSet.RunCompleted += creativeSet_RunCompleted;
         runningCreativeSet.RunAsync(creativesToMerge);
     }
     else
     {
         if (!CloseCompanionsOnComplete && runningCreativeSet != null && runningCreativeSet.RunningCreatives != null)
         {
             // don't cleanup, let everything continue to run
             OnCompleted(true);
         }
         else
         {
             Cleanup();
             OnCompleted(true);
         }
     }
 }
        private void creativeSet_RunCompleted(CreativeSet sender, bool success)
        {
            var vastCreativeSet = sender as VastCreativeSet;

            // cleanup
            foreach (var creative in vastCreativeSet.Creatives)
            {
                creative.RunCompleted -= creative_RunCompleted;
            }
            activeCreativeSets.Remove(vastCreativeSet);
        }
        /// <summary>
        /// Cancels all running CreativeSets and cleans up.
        /// </summary>
        protected virtual void Cleanup()
        {
            // clean up
            pendingCreativeSets = null;

            if (runningCreativeSet != null)
            {
                runningCreativeSet.RunCompleted -= creativeSet_RunCompleted;
                runningCreativeSet.Cancel();
                runningCreativeSet = null;
            }
        }
        //protected override void RefreshAdMode()
        //{
        //    if (activeCreativeSets.Any(cs => cs.ContainsLinear) || VpaidController.ActivePlayers.Any(p => p.AdLinear))    // if any creative sets contain a linear ad, we need to block playback
        //    {
        //        BlockPlayer();
        //    }
        //    else
        //    {
        //        ReleasePlayer();
        //    }
        //}

        //protected override bool IsPauseRequired()
        //{
        //    var linearPlayers = VpaidController.ActivePlayers.Where(p => p.AdLinear).ToList();
        //    return (linearPlayers.Any() && !linearPlayers.Any(p => (p is IVpaidLinearBehavior && ((IVpaidLinearBehavior)p).Nonlinear)));
        //}

        #endregion

        #region AdModel EventHandlers
        private void creativeSet_RunStarting(CreativeSet sender)
        {
            var vastCreativeSet = sender as VastCreativeSet;

            activeCreativeSets.Add(vastCreativeSet);    // remember the active creative set.
            //RefreshAdMode();

            foreach (var creative in vastCreativeSet.Creatives)
            {
                creative.RunCompleted += creative_RunCompleted;
            }
        }
 private void creativeSet_RunCompleted(CreativeSet creative, bool success)
 {
     creative.RunCompleted -= creativeSet_RunCompleted;
     if (!success && (FailureStrategy != FailurePolicyEnum.Ignore || !pendingCreativeSets.Any()))
     {
         Cleanup();
         OnCompleted(success);
     }
     else
     {
         RunNextInQueue();
     }
 }
        private void creativeSet_RunStarted(CreativeSet sender)
        {
            var vastCreativeSet = sender as VastCreativeSet;

            // unhook all creatives
            foreach (var creative in vastCreativeSet.Creatives)
            {
                creative.RunCompleted -= creative_RunCompleted;
            }
            // re-hook the ones that are actually running
            foreach (var creative in vastCreativeSet.RunningCreatives)
            {
                creative.RunCompleted += creative_RunCompleted;
            }
        }
 private void creativeSet_RunCompleted(CreativeSet sender, bool success)
 {
     var vastCreativeSet = sender as VastCreativeSet;
     // cleanup
     foreach (var creative in vastCreativeSet.Creatives)
     {
         creative.RunCompleted -= creative_RunCompleted;
     }
     activeCreativeSets.Remove(vastCreativeSet);
 }
 private void creativeSet_RunStarted(CreativeSet sender)
 {
     var vastCreativeSet = sender as VastCreativeSet;
     // unhook all creatives
     foreach (var creative in vastCreativeSet.Creatives)
     {
         creative.RunCompleted -= creative_RunCompleted;
     }
     // re-hook the ones that are actually running
     foreach (var creative in vastCreativeSet.RunningCreatives)
     {
         creative.RunCompleted += creative_RunCompleted;
     }
 }
        //protected override void RefreshAdMode()
        //{
        //    if (activeCreativeSets.Any(cs => cs.ContainsLinear) || VpaidController.ActivePlayers.Any(p => p.AdLinear))    // if any creative sets contain a linear ad, we need to block playback
        //    {
        //        BlockPlayer();
        //    }
        //    else
        //    {
        //        ReleasePlayer();
        //    }
        //}

        //protected override bool IsPauseRequired()
        //{
        //    var linearPlayers = VpaidController.ActivePlayers.Where(p => p.AdLinear).ToList();
        //    return (linearPlayers.Any() && !linearPlayers.Any(p => (p is IVpaidLinearBehavior && ((IVpaidLinearBehavior)p).Nonlinear)));
        //}

        #endregion

        #region AdModel EventHandlers
        private void creativeSet_RunStarting(CreativeSet sender)
        {
            var vastCreativeSet = sender as VastCreativeSet;
            activeCreativeSets.Add(vastCreativeSet);    // remember the active creative set.
            //RefreshAdMode();

            foreach (var creative in vastCreativeSet.Creatives)
            {
                creative.RunCompleted += creative_RunCompleted;
            }
        }
        /// <summary>
        /// Cancels all running CreativeSets and cleans up.
        /// </summary>
        protected virtual void Cleanup()
        {
            // clean up
            pendingCreativeSets = null;

            if (runningCreativeSet != null)
            {
                runningCreativeSet.RunCompleted -= creativeSet_RunCompleted;
                runningCreativeSet.Cancel();
                runningCreativeSet = null;
            }
        }
 private void creativeSet_RunCompleted(CreativeSet creative, bool success)
 {
     creative.RunCompleted -= creativeSet_RunCompleted;
     if (!success && (FailureStrategy != FailurePolicyEnum.Ignore || !pendingCreativeSets.Any()))
     {
         Cleanup();
         OnCompleted(success);
     }
     else
     {
         RunNextInQueue();
     }
 }
 private void RunNextInQueue()
 {
     if (pendingCreativeSets.Any())
     {
         IEnumerable<Creative> creativesToMerge;
         if (runningCreativeSet != null && runningCreativeSet.RunningCreatives != null)
         {
             creativesToMerge = runningCreativeSet.RunningCreatives;
             runningCreativeSet.RunningCreatives = null;
         }
         else
         {
             creativesToMerge = Enumerable.Empty<Creative>();
         }
         runningCreativeSet = pendingCreativeSets.Dequeue();
         runningCreativeSet.RunCompleted += creativeSet_RunCompleted;
         runningCreativeSet.RunAsync(creativesToMerge);
     }
     else
     {
         if (!CloseCompanionsOnComplete && runningCreativeSet != null && runningCreativeSet.RunningCreatives != null)
         {
             // don't cleanup, let everything continue to run
             OnCompleted(true);
         }
         else
         {
             Cleanup();
             OnCompleted(true);
         }
     }
 }