Search code examples
c#loadingstack-overflow

Loading Page -> Stack Overflow


OK so, i'm simply trying to create a basic loading page, so i have some sexy page appear (doesn't do any loading) just shows up for a couple seconds before my real form appears

this is my code:

  public partial class LoadingPage : Window
{
    System.Threading.Thread iThread;

    public LoadingPage()
    {
        InitializeComponent();
    }

    private void Refresh()
    {
        System.Threading.Thread.Sleep(900);
        MainWindow iMain = new MainWindow();
        iMain.ShowDialog();
        this.Dispatcher.Invoke(new Action(Close));
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    iThread = new System.Threading.Thread(new ThreadStart(Refresh));
    iThread.SetApartmentState(System.Threading.ApartmentState.STA);
    iThread.Start();   
    }

    private void Close()
    {
        this.Close();
    }

This works, but causes a stack overflow and doesn't close the loading window when the main page opens..

furthermore, the close method has a green underline saying something about 'Hiding inherited member System.Window.Windows.Close() use the new keyword if hiding was intended'

The question is : What is causing the stack overflow?


Solution

  • In

    private void Close()
    {
       this.Close();
    }
    

    You are calling the same Close in an infinite recursion, which overflows the stack

    I think you meant

    private void Close()
    {
       base.Close();
    }