Search code examples
uwpunhandled-exception

UWP System.StackOverflowException


I'm creating a news feed To test it, I use combobox_selectionChanged event to activate this function, when selectedItem=0 shows world headlines, selectedItem=1 shows sports headlines, selectedItem=2 change the visibility to collapsed. it can work when you select item0 or change to item1, but once I change to item2,it shows me system.StackOverflowException

Here is my code `

CancellationTokenSource cts = null;
    public async void NewsRepeat()
    {          
        cts?.Cancel();
        try
        {
            var localCts = cts = new CancellationTokenSource();                           
            localCts.Token.ThrowIfCancellationRequested();
            if (newsTpye.SelectedIndex == 0)
            {
                NewsPanel.Visibility = Visibility.Visible;
                RootObject2 myNews = await NewsProxy.GetNews();
                newsChannel.Text = "World Headlines";
                for (k = 0; k <= 8; k++)
                {
                    if (myNews.articles[k].title != null)
                        showTitle.Text = myNews.articles[k].title;
                    else
                        showTitle.Text = "";
                    if (myNews.articles[k].urlToImage != null)
                        newsImage.Source = new BitmapImage(new Uri(myNews.articles[k].urlToImage, UriKind.Absolute));
                    else
                        newsImage.Source = new BitmapImage(new Uri("ms-appx:///Assets/image/NoPic.jpg", UriKind.RelativeOrAbsolute));
                    if (myNews.articles[k].publishedAt != null)
                        showTime.Text = myNews.articles[k].publishedAt;
                    else
                        showTime.Text = "";
                    showDescription.Text = "(" + myNews.source + "): " + myNews.articles[k].description;
                    await Task.Delay(5000);
                    localCts.Token.ThrowIfCancellationRequested();
                }

            }
            else if (newsTpye.SelectedIndex == 1)
            {
                NewsPanel.Visibility = Visibility.Visible;
                RootObject3 mySportNews = await sportsNewsProxy.GetSportNews();
                newsChannel.Text = "Sports Headlines";

                for (k = 0; k <= 8; k++)
                {
                    if (mySportNews.articles[k].title != null)
                        showTitle.Text = mySportNews.articles[k].title;
                    else
                        showTitle.Text = "";

                    if (mySportNews.articles[k].urlToImage != null)
                        newsImage.Source = new BitmapImage(new Uri(mySportNews.articles[k].urlToImage, UriKind.Absolute));
                    else
                        newsImage.Source = new BitmapImage(new Uri("ms-appx:///Assets/image/NoPic.jpg", UriKind.RelativeOrAbsolute));

                    if (mySportNews.articles[k].publishedAt != null)
                        showTime.Text = mySportNews.articles[k].publishedAt;
                    else
                        showTime.Text = "";
                    showDescription.Text = "(" + mySportNews.source + "): " + mySportNews.articles[k].description;
                    await Task.Delay(5000);
                    localCts.Token.ThrowIfCancellationRequested();
                }
            }
            else if (newsTpye.SelectedIndex == 2)
            {
                NewsPanel.Visibility = Visibility.Collapsed;
            }
            NewsRepeat();
        }
        catch (OperationCanceledException)
        {
            // Swallow this exception only - this is probably
            // the one we've thrown ourselves
        } 
    }

    private void newsType_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        NewsRepeat();
    }`

enter image description here


Solution

  • Each time you call a method, you increase your stack usage. When SelectedIndex == 2, your method will just call itself over and over again, causing a stack overflow.

    could you teach how to exit out ?

    Use the return statement.

    On a side note, you should be using async Task. async void is mainly for event handlers.