1

What is the best way to update splash sceen labels on application startup, to inform the user what's going on ? The problem is that the splash screen is created in an override method, while updating has to be done within the static main method, which can't access "this.SplashScreen".

class SingleInstanceApplication : WindowsFormsApplicationBase
{
    [STAThread]
    static void Main(string[] args)
    {
        SetSplashInfo("Data configuration", "Applying DataDirectory"); 
        //Can't be done, this method is static**
        //Do some stuff, code removed for reading purposes
    }

    protected override void OnCreateSplashScreen()
    {
        this.SplashScreen = new TestSplash();
        this.SplashScreen.TopMost = true; 

        base.OnCreateSplashScreen();
    }

    private void SetSplashInfo(string txt1, string txt2)
    {
        if (  this.SplashScreen == null)
            return;
       TestSplash splashFrm = (TestSplash)this.SplashScreen;
        splashFrm.label1.Text = txt1;
        splashFrm.label2.Text = txt2;
    }
}
Run CMD
  • 2,937
  • 3
  • 36
  • 61

1 Answers1

2

Yes, you need a reference to the SingleInstanceApplication object. Since there is only ever one of them, you can cheat:

class SingleInstanceApplication : WindowsFormsApplicationBase {
    private static SingleInstanceApplication instance;
    public SingleInstanceApplication() {
       instance = this;
    }
}

Now you can use instance.SplashScreen to always get a reference to the splash screen and make SetSplashInfo() static. A clean fix should be possible but I can't see how you are creating the SingleInstanceApplication instance.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • thanks, but that doesn't seem to work. Either 'instance' is null (in Main) or you get a 'Cross-thread operation not valid' if accessing instance from within 'override void OnCreateMainForm' ... – Run CMD Dec 16 '10 at 15:53
  • 1
    You can only use it *after* creating the SingleInstanceApplication object. Yes, you *have* to use the BeginInvoke() method on the SplashScreen, it runs in another thread. Check the docs for Control.BeginInvoke() for help. – Hans Passant Dec 16 '10 at 15:56
  • sorry .. that was pretty stupid ... :-) .. thanks for all the help! – Run CMD Dec 16 '10 at 16:02