I have two UserControls which added in a form in my winforms project, one with the function to draw a string and make it scrolling from right to left and the other draw another string and make it scroll from bottom to top over and over again controlled by calling Invalidate() inside a while(true) loop.But when one of my string becomes too long, about 1000 characters the UI Thread is blocked, so my questions are: What am I doing wrong? is there a better way to make text scrolling??? and bellow is my snippet of code:
- int scrollTextSpeed = 100;
- bool scrollingText = true;
- Thread updateUI ;
- void init()
- {
- updateUI = new Thread(updateScrollText);
- updateUI.Start(); } void updateScrollText() { while (true)
- {
- if (scrollingText)
- {
- Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Normal, new MethodInvoker(Invalidate)); Thread.Sleep(scrollTextSpeed);
- }
- }
- }
- protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
- {
- SizeF stringSize = e.Graphics.MeasureString(text, this.Font);
- var yPos = (this.ClientSize.Height / 2) - (stringSize.Height / 2);
- e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias; e.Graphics.DrawString(text, this.Font, brus, currentPos, yPos);
- if (fisttime)
- {
- currentPos = this.ClientSize.Width - 1;
- fisttime = false;
- }
- else
- {
- if (currentPos < (-1 * (stringSize.Width)))
- currentPos = this.ClientSize.Width - 1;
- else
- currentPos -= scrollPixelDistance;
- }
- }