0

My application is hardware based so it have connection in first form's contructor called myform. i want to display splash screen until loads myform in background. application in c sharp. after splash screen login will display.

Willem van Rumpt
  • 6,490
  • 2
  • 32
  • 44
Akshay Khade
  • 57
  • 1
  • 11

1 Answers1

0

Check my SimpleSplash implementation.

public sealed class SplashForm<T> : Form
    where T : Form
{
    public SplashForm()
    {
        SuspendLayout();

        Size = new Size(350, 100);
        StartPosition = FormStartPosition.CenterScreen;
        FormBorderStyle = FormBorderStyle.None;

        var lblMessage = new Label
        {
            Location = new Point(10, 10),
            Text = "loading..."
        };

        Controls.Add(lblMessage);

        ResumeLayout(false);
        PerformLayout();

        LoadMainForm();
    }

    private async void LoadMainForm()
    {
        Form instance = null;
        await Task.Run(() =>
        {
            //--get the main form type
            var firstFormType = GetType().GenericTypeArguments.First();
            //--create the instance
            instance = Activator.CreateInstance(firstFormType) as Form;
        });

        if (instance == null)
            throw new InvalidOperationException("form initialization error");

        OnMainFormLoaded(instance);
    }

    public event EventHandler<Form> MainFormLoaded;

    private void OnMainFormLoaded(Form formInstance)
    {
        EventHandler<Form> handler = MainFormLoaded;
        if (handler != null)
            handler(this, formInstance);
    }
}

The below code is a sample form implementation:

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();

        Thread.Sleep(Environment.TickCount%1500); //--simulate variables initialization
    }
}

You need to make some adjustments in application entry point.

internal static class Program
{
    [STAThread]
    private static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Form mainFormInstance = null;

        var splash = new SplashForm<MainForm>();
        splash.MainFormLoaded +=
            (s, e) =>
            {
                mainFormInstance = e;
                splash.Dispose();
            };

        Application.Run(splash);
        Application.Run(mainFormInstance);
    }
}

I hope it helps.

denys-vega
  • 3,522
  • 1
  • 19
  • 24