UserControl performance

May 24 2007 8:34 AM

I designed the following user control to scroll SMS messages, one instance of it will work fine but when I declared two on the same form it became very very slow. Here is the OnPaint function and another function for processing smileys.

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
   base.OnPaint(e);
       
            if (bGradientEnabled)
            {
                //  Draw the background
                LinearGradientBrush bgbrush1 = new LinearGradientBrush(new Rectangle(0, 0, Width / 2, Height), BackColor, Color.White, LinearGradientMode.Horizontal);
                LinearGradientBrush bgbrush2 = new LinearGradientBrush(new Rectangle((Width / 2) - 1, 0, Width / 2, Height), Color.White, BackColor, LinearGradientMode.Horizontal);
                e.Graphics.FillRectangle(bgbrush1, 0, 0, Width / 2, Height);
                e.Graphics.FillRectangle(bgbrush2, Width / 2, 0, Width / 2, Height);
            }

            if (bBorderLineEnabled)
            {
                //  Draw the upper and the lower lines
                Pen pen = new Pen(clrBorderLineColor, nBorderLineHeight);
                e.Graphics.DrawLine(pen, 0, 0, Width, 0);
                e.Graphics.DrawLine(pen, 0, Height, Width, Height);
            }
           
            if (bRTL)
            {
                //******************************** Draw String ***********************************************
                if ((nPosition + flMessageWidth) < 0)
                {
                    nPosition = Width;
                }

                float flPaintedPortionsWidth = 0;

                for (int i = 0; i <= mDisplayingMsgQueue.Count - 1; i++)
                {
                    if (((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).PortionType == MessagePortionType.TextContent)
                    {
                        e.Graphics.DrawString(((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).PortionContentAsString, Font, new SolidBrush(ForeColor), nPosition + flPaintedPortionsWidth,
                            (this.Height / 2) - (e.Graphics.MeasureString(((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).PortionContentAsString, Font, new PointF(0, 0), StringFormat.GenericTypographic).Height / 2), StringFormat.GenericTypographic);
                        flPaintedPortionsWidth += ((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).GetPortionWidth(Font);
                    }
                    else
                    {
                        e.Graphics.DrawImage(((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).PortionContentAsImage, nPosition + flPaintedPortionsWidth,
                            (this.Height / 2) - (((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).PortionContentAsImage.Height / 2));
                        flPaintedPortionsWidth += ((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).GetPortionWidth(Font);
                    }
                }
                //********************************************************************************************

                //******************************** Delete Message ********************************************
                if (mDisplayingMsgQueue.Count > 1)
                {
                    if ((flMessageWidth >= Width) && ((nPosition + ((MessagePortion)(mDisplayingMsgQueue.Peek())).GetPortionWidth(Font)) <= 0))
                    {
                        MessagePortion msg = (MessagePortion)mDisplayingMsgQueue.Dequeue();
                        mOldMsgQueue.Enqueue(msg);
                        flMessageWidth -= ((MessagePortion)(msg)).GetPortionWidth(Font);
                        nPosition += ((MessagePortion)(msg)).GetPortionWidth(Font);

                        if (mNewMsgQueue.Count <= 2)
                        {
                            EventArgs ea = new EventArgs();
                            OnMessageQueueEmpty(ea);
                        }
                    }
                }
                //********************************************************************************************

                //******************************** Add Message ***********************************************
                if (((nPosition + flMessageWidth) <= Width) || mDisplayingMsgQueue.Count == 0)
                {
                    if (mNewMsgQueue.Count == 0)
                    {
                        isNewStarted = false;
                    }

                    if (mNewMsgQueue.Count > 0 && mDisplayingMsgQueue.Count == 0 && mOldMsgQueue.Count == 0)
                    {
                        MessagePortion msg = (MessagePortion)mNewMsgQueue.Dequeue();
                        mDisplayingMsgQueue.Enqueue(msg);
                        flMessageWidth += ((MessagePortion)(msg)).GetPortionWidth(Font);
                        isNewStarted = true;
                    }
                    else if (mNewMsgQueue.Count > 0 && mDisplayingMsgQueue.Count > 0 && mOldMsgQueue.Count == 0)
                    {
                        MessagePortion msg = (MessagePortion)mNewMsgQueue.Dequeue();
                        mDisplayingMsgQueue.Enqueue(msg);
                        flMessageWidth += ((MessagePortion)(msg)).GetPortionWidth(Font);
                        isNewStarted = true;
                    }
                    else if (mNewMsgQueue.Count == 0 && mDisplayingMsgQueue.Count > 0 && mOldMsgQueue.Count > 0)
                    {
                        MessagePortion msg = (MessagePortion)mOldMsgQueue.Dequeue();
                        mDisplayingMsgQueue.Enqueue(msg);
                        flMessageWidth += ((MessagePortion)(msg)).GetPortionWidth(Font);
                    }
                    else if (mNewMsgQueue.Count > 0 && mDisplayingMsgQueue.Count > 0 && mOldMsgQueue.Count > 0)
                    {
                        if (((MessagePortion)(mDisplayingMsgQueue.ToArray()[mDisplayingMsgQueue.Count - 1])).PortionType == MessagePortionType.LogoContent)
                        {
                            MessagePortion msg = (MessagePortion)mNewMsgQueue.Dequeue();
                            mDisplayingMsgQueue.Enqueue(msg);
                            flMessageWidth += ((MessagePortion)(msg)).GetPortionWidth(Font);
                            isNewStarted = true;
                        }
                        else if (isNewStarted)
                        {
                            MessagePortion msg = (MessagePortion)mNewMsgQueue.Dequeue();
                            mDisplayingMsgQueue.Enqueue(msg);
                            flMessageWidth += ((MessagePortion)(msg)).GetPortionWidth(Font);
                        }
                        else
                        {
                            MessagePortion msg = (MessagePortion)mOldMsgQueue.Dequeue();
                            mDisplayingMsgQueue.Enqueue(msg);
                            flMessageWidth += ((MessagePortion)(msg)).GetPortionWidth(Font);
                        }
                    }
                }
                //********************************************************************************************
            }
            else
            {
                //******************************** Draw String ***********************************************
                if ((nPosition - flMessageWidth) > Width)
                {
                    nPosition = 0;
                }

                float flPaintedPortionsWidth = 0;

                for (int i = mDisplayingMsgQueue.Count - 1; i >= 0; i--)
                {
                    if (((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).PortionType == MessagePortionType.TextContent)
                    {
                        e.Graphics.DrawString(((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).PortionContentAsString, Font, new SolidBrush(ForeColor),
                            nPosition - flMessageWidth + flPaintedPortionsWidth,
                            (this.Height / 2) - (e.Graphics.MeasureString(((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).PortionContentAsString, Font, new PointF(0, 0), StringFormat.GenericTypographic).Height / 2), StringFormat.GenericTypographic);
                        flPaintedPortionsWidth += ((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).GetPortionWidth(Font);
                    }
                    else
                    {
                        PointF ulCorner = new PointF(nPosition - flMessageWidth + flPaintedPortionsWidth, (this.Height / 2) - (((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).PortionContentAsImage.Height / 2));
                        PointF urCorner = new PointF(nPosition - flMessageWidth + flPaintedPortionsWidth + ((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).GetPortionWidth(Font), (this.Height / 2) - (((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).PortionContentAsImage.Height / 2));
                        PointF llCorner = new PointF(nPosition - flMessageWidth + flPaintedPortionsWidth, (this.Height / 2) - (((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).PortionContentAsImage.Height / 2) + ((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).PortionContentAsImage.Height);
                        PointF[] destPara = {ulCorner, urCorner, llCorner};

                        e.Graphics.DrawImage(((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).PortionContentAsImage, destPara);
                        flPaintedPortionsWidth += ((MessagePortion)(mDisplayingMsgQueue.ToArray()[i])).GetPortionWidth(Font);
                    }
                }
                //********************************************************************************************

                //******************************** Delete Message ********************************************
                if (mDisplayingMsgQueue.Count > 0)
                {
                    if ((flMessageWidth >= Width) && ((nPosition - ((MessagePortion)(mDisplayingMsgQueue.Peek())).GetPortionWidth(Font)) >= Width))
                    {
                        //MessageBox.Show("dd");
                        MessagePortion msg = (MessagePortion)mDisplayingMsgQueue.Dequeue();
                        mOldMsgQueue.Enqueue(msg);
                        flMessageWidth -= ((MessagePortion)(msg)).GetPortionWidth(Font);
                        nPosition -= ((MessagePortion)(msg)).GetPortionWidth(Font);
                       
                        if (mNewMsgQueue.Count <= 2)
                        {
                            EventArgs ea = new EventArgs();
                            OnMessageQueueEmpty(ea);
                        }
                    }
                }
                //********************************************************************************************
               
                //******************************** Add Message ***********************************************
                if (((nPosition - flMessageWidth) >= 0) || (mDisplayingMsgQueue.Count == 0))
                {
                    if (mNewMsgQueue.Count == 0)
                    {
                        isNewStarted = false;
                    }
             
                    if (mNewMsgQueue.Count > 0 && mDisplayingMsgQueue.Count == 0 && mOldMsgQueue.Count == 0)
                    {
                        MessagePortion msg = (MessagePortion)mNewMsgQueue.Dequeue();
                        mDisplayingMsgQueue.Enqueue(msg);
                        flMessageWidth += ((MessagePortion)(msg)).GetPortionWidth(Font);
                        isNewStarted = true;
                    }
                    else if (mNewMsgQueue.Count > 0 && mDisplayingMsgQueue.Count > 0 && mOldMsgQueue.Count == 0)
                    {
                        MessagePortion msg = (MessagePortion)mNewMsgQueue.Dequeue();
                        mDisplayingMsgQueue.Enqueue(msg);
                        flMessageWidth += ((MessagePortion)(msg)).GetPortionWidth(Font);
                        isNewStarted = true;
                    }
                    else if (mNewMsgQueue.Count == 0 && mDisplayingMsgQueue.Count > 0 && mOldMsgQueue.Count > 0)
                    {
                        MessagePortion msg = (MessagePortion)mOldMsgQueue.Dequeue();
                        mDisplayingMsgQueue.Enqueue(msg);
                        flMessageWidth += ((MessagePortion)(msg)).GetPortionWidth(Font);
                    }
                    else if (mNewMsgQueue.Count > 0 && mDisplayingMsgQueue.Count > 0 && mOldMsgQueue.Count > 0)
                    {
                        if (((MessagePortion)(mDisplayingMsgQueue.ToArray()[mDisplayingMsgQueue.Count - 1])).PortionType == MessagePortionType.LogoContent)
                        {
                            MessagePortion msg = (MessagePortion)mNewMsgQueue.Dequeue();
                            mDisplayingMsgQueue.Enqueue(msg);
                            flMessageWidth += ((MessagePortion)(msg)).GetPortionWidth(Font);
                            isNewStarted = true;
                        }
                        else if(isNewStarted)
                        {
                            MessagePortion msg = (MessagePortion)mNewMsgQueue.Dequeue();
                            mDisplayingMsgQueue.Enqueue(msg);
                            flMessageWidth += ((MessagePortion)(msg)).GetPortionWidth(Font);
                        }
                        else
                        {
                            MessagePortion msg = (MessagePortion)mOldMsgQueue.Dequeue();
                            mDisplayingMsgQueue.Enqueue(msg);
                            flMessageWidth += ((MessagePortion)(msg)).GetPortionWidth(Font);
                        }
                    }
                }
                //********************************************************************************************
            }

            e.Graphics.DrawImage(global::SMS2TV.Properties.Resources.box_down, new Rectangle(this.Width - ((int)(this.Height * 1.5)), 0, (int)(this.Height * 1.5), this.Height));
            e.Graphics.DrawString(strShortcode, new Font(new FontFamily(this.Font.FontFamily.Name.ToString()), 30, FontStyle.Bold), new SolidBrush(Color.Black), this.Width - ((int)(this.Height * 1.5)) + ((int)(90 / 1.5)), ((int)(90 / 1.5)), StringFormat.GenericTypographic);
  }
  
  private System.Collections.ArrayList SplitMessage(System.Collections.ArrayList paramList, string[] CommandList, int Position, DateTime timestamp)
        {
            System.Collections.ArrayList list = new System.Collections.ArrayList();
            Graphics g = this.CreateGraphics();
            string strSentence = "";

            if (Position == CommandList.Length)
                return paramList;

            for (int i = 0; i <= paramList.Count - 1; i++)
            {
                if (((MessagePortion)(paramList.ToArray()[i])).PortionType == MessagePortionType.TextContent)
                {
                    strSentence = ((MessagePortion)(paramList.ToArray()[i])).PortionContentAsString;

                    while (strSentence.Length != 0)
                    {
                        if (!strSentence.Contains(CommandList[Position]))
                        {
                            list.Add(new MessagePortion(strSentence, g, MessagePortionType.TextContent, timestamp));
                            strSentence = "";
                        }

                        else if (strSentence.Equals(CommandList[Position]))
                        {
                            list.Add(new MessagePortion(mSmiley.GetImageByCommand(CommandList[Position]), g, MessagePortionType.IconContent, timestamp));
                            strSentence = "";
                        }

                        else
                        {
                            if (!strSentence.Substring(0, strSentence.IndexOf(CommandList[Position])).Equals(""))
                                list.Add(new MessagePortion(strSentence.Substring(0, strSentence.IndexOf(CommandList[Position])),
                                    g, MessagePortionType.TextContent, timestamp));

                            list.Add(new MessagePortion(mSmiley.GetImageByCommand(CommandList[Position]), g, MessagePortionType.IconContent, timestamp));
                            strSentence = strSentence.Substring(strSentence.IndexOf(CommandList[Position]) + CommandList[Position].Length,
                                strSentence.Length - strSentence.IndexOf(CommandList[Position]) - CommandList[Position].Length);
                        }
                    }
                }
                else
                {
                    list.Add((((MessagePortion)(paramList.ToArray()[i]))));
                }
            }

            return SplitMessage(list, CommandList, Position + 1, timestamp);
        }

Is this a threading model issue? Any help?