0

When making a label scroll, when the information is to large, I get a StackOverflow Exception.

Code in question:

If anyone knows of a fix, please answer below.

private async Task LblScroll(int time)
{
    lblScroll.Location = new Point(lblScroll.Location.X - 5, lblScroll.Location.Y);
    if (lblScroll.Location.X < (-lblScroll.Width))
    {
        if (stopScroll)
            return;
        else
            lblScroll.Location = new Point(this.ClientSize.Width + lblScroll.Text.Length, lblScroll.Location.Y);
    }

    if (!stopScroll)
    {
        await Task.Delay(time);
        await LblScroll(time);
    }
    else
        return;
}
madreflection
  • 4,744
  • 3
  • 19
  • 29
Moonbase2004
  • 11
  • 1
  • 3
  • Whats the rest of the code. Is this an event? is this the method name `LblScroll` ? – TheGeneral Feb 17 '20 at 22:18
  • 2
    If i had to take a guess, you are recursively calling `LblScroll` this is likely the wrong approach. use a timer or a loop – TheGeneral Feb 17 '20 at 22:22
  • @MichaelRandall: the declaration was on the same line as the backticks so they weren't rendered. Your suspicion is right on. – madreflection Feb 17 '20 at 22:22
  • @madreflection ahh ok, that makes more sense – TheGeneral Feb 17 '20 at 22:23
  • Does this answer your question? [StackOverFlow in recursive function C#](https://stackoverflow.com/questions/22025074/stackoverflow-in-recursive-function-c-sharp). See also: [Elegant way to prevent StackOverflow in Recursion](https://stackoverflow.com/q/43591369/8967612). – 41686d6564 stands w. Palestine Feb 17 '20 at 22:33
  • [How to make label reappear as soon as it's going out of panel width](https://stackoverflow.com/a/56297397/7444103) – Jimi Feb 17 '20 at 23:29

1 Answers1

0

The issue has been resolved.

I had to switch from using awaits to timers. This worked like a charm.

Moonbase2004
  • 11
  • 1
  • 3