/// <summary>
 /// 计算弹幕等待时间(按刷新步数)。
 /// </summary>
 /// <param name="barrage">将要新插入的弹幕。</param>
 /// <returns>一个int值,表示经过该值对应的时间后弹幕将显示。</returns>
 public int WaitingTime(Barrage barrage)
 {
     if (queue.Count == 0)
         return 0;
     Barrage last = queue.Last();
     if (last.Speed >= barrage.Speed)
     {
         int w = last.WaitingTime + last.Width / last.Speed + 1;
         return w < 0 ? 0 : w;
     }
     int diffSpeed = barrage.Speed - last.Speed;
     int lastCur = -last.WaitingTime * last.Speed;
     int conflict = parent.size.Width - parent.size.Width / barrage.Speed * last.Speed;
     if (conflict < lastCur)
         return 0;
     return (conflict - lastCur) / last.Speed;
 }
 private void InternalAddBarrage(Barrage barrage)
 {
     barrage.WaitingTime = 0;
     barrage.Speed = speed;
     int normalTime = (barrage.Width + size.Width) / speed;
     if (normalTime > maxTime)
         barrage.Speed = (barrage.Width + size.Width) / maxTime;
     int min = 0;
     int minWaitingTime = Int32.MaxValue;
     for (int i = 0; i < barrageLines.Length; i++)
     {
         int waitingTime = barrageLines[i].WaitingTime(barrage);
         if (waitingTime < minWaitingTime)
         {
             min = i;
             minWaitingTime = waitingTime;
         }
     }
     if (minWaitingTime > maxWait)
         return;
     barrage.X = size.Width;
     barrage.Y = min * barrageHeight;
     barrageLines[min].AddBarrage(barrage);
     count++;
 }
 /// <summary>
 /// 添加一条新弹幕。弹幕的等待时间将被修改为在该BarrageLine中的等待时间。
 /// </summary>
 /// <param name="barrage">将要新插入的弹幕。</param>
 public void AddBarrage(Barrage barrage)
 {
     barrage.WaitingTime = WaitingTime(barrage);
     queue.Enqueue(barrage);
 }
        /// <summary>
        /// 添加一条新的弹幕评论。
        /// </summary>
        /// <param name="comment">将要添加的弹幕评论内容。</param>
        public void AddBarrage(string comment)
        {
            if (count >= maxCount || comment.Length > maxLength)
                return;

            AsyncOperation op = AsyncOperationManager.CreateOperation(Guid.NewGuid());
            Thread thread = new Thread(() =>
            {
                bool entered = false;
                Barrage barrage = null;
                try
                {
                    entered = Monitor.TryEnter(renderer, maxRenderWait);
                    if (!entered)
                    {
                        Debugger.Log("等待绘制超时: " + comment);
                        return;
                    }
                    Bitmap bitmap = renderer.RenderBarrage(comment, barrageStyle);

                    barrage = new Barrage(bitmap);
                    bitmap.Dispose();
                }
                catch (Exception e)
                {
                    Debugger.Exception(e);
                }
                finally
                {
                    if (entered)
                        Monitor.Exit(renderer);
                    if (barrage != null)
                    {
                        op.PostOperationCompleted(o =>
                        {
                            InternalAddBarrage(barrage);
                            Debugger.Log("弹幕: " + comment);
                        }, null);
                    }
                }
            });
            thread.Priority = ThreadPriority.BelowNormal;
            thread.Start();
        }