Close with timeout is not working in Console to close splashscreen

Viewed 431

I have made a class, I make an instance of. In said instance I have these lines of code to show and close the splashscreen.

// Open (show)
public void ShowSplashScreen(bool autoClose = false)
{
    splashscreen.Show(autoClose, true);
}

// Close (don't show)
public void CloseSplashScreen()
{
    splashscreen.Close(TimeSpan.FromSeconds(0.3));
}

It shows up fine, but never closes, just stays there.

This is the documentation of splashscreen Close: https://docs.microsoft.com/en-us/dotnet/api/system.windows.splashscreen.close?view=netframework-4.8

[System.Security.SecurityCritical]

public void Close (TimeSpan fadeoutDuration);

Note: I am using the show method with the parameters AutoClose set to false, and TopMost set to true, this makes it not auto close as I want to close it programmatically and not subscribe to existing events.


I am running the lines of code from a Console (.NET framework) application for testing purposes before implementing it into my UI fully.


What I have tried: Debugging and even trying to call show again before calling close.


It is definitely something going wrong with the class, as calling the class and directly manipulating the property works:

ClassSplashScreen rss = new ClassSplashScreen();
rss.splashscreen.Show(false);
rss.splashscreen.Close(TimeSpan.FromSeconds(1));

My best guess is something is hanging the UI and freezing it? But I am unsure what to do about it.


Code to run to test this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace NamespaceName
{
    public class StackOverFlowCode
    {
        static void Main(string[] args)
        {
            ClassSplashScreen screen = new ClassSplashScreen();
            screen.ShowSplashScreen();
            screen.CloseSplashScreen();
        }

    }
    public class ClassSplashScreen
    {
        public SplashScreen splashscreen { get; set; }
        public ClassSplashScreen()
        {
            splashscreen = new SplashScreen("Resource Image Link");
        }
        public void ChangeSplashResource(SplashScreen resource)
        {
            splashscreen = resource;
        }
        public void ShowSplashScreen(bool autoClose = false)
        {
            splashscreen.Show(autoClose, true);
        }
        public void CloseSplashScreen()
        {
            splashscreen.Close(TimeSpan.FromSeconds(1));
        }
    }
}
1 Answers

The SplashScreen relies on a dispatcher but there is no one in a console application by default. If you create a System.Windows.Application, it should work as expected:

public class StackOverFlowCode
{
    [STAThread]
    static void Main(string[] args)
    {
        Application app = new Application();
        app.Startup += (s, e) => 
        {
            ClassSplashScreen screen = new ClassSplashScreen();
            screen.ShowSplashScreen();
            screen.CloseSplashScreen();
        };
        app.Run();
    }
}

public class ClassSplashScreen
{
    private readonly SplashScreen splashscreen;

    public ClassSplashScreen() => splashscreen = new SplashScreen("Resource Image Link");

    public void ShowSplashScreen() => splashscreen.Show(false);

    public void CloseSplashScreen() => splashscreen.Close(TimeSpan.FromSeconds(1));
}
Related