So I have a splash screen which will have some time intensive code and I don't want it running in the main thread. I have made some code which should stop the thread and close the form, but it does not work. Any help is welcomed.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace Cobalt
{
partial class Cobalt : Form
{
public static bool splashCont { get; set; }
public Cobalt()
{
this.Text = "Cobalt V1.0.0";
this.Width = 400;
this.Height = 100;
this.BackgroundImage = Properties.Resources.cobaltlgo;
this.FormBorderStyle = FormBorderStyle.None;
this.TopMost = true;
this.StartPosition = FormStartPosition.CenterScreen;
Thread splash = new Thread(new ThreadStart(splashLoadAction));
splash.Start();
if (splashCont)
{
splash.Abort();
this.Close();
}
}
private void splashLoadAction()
{
Thread.Sleep(5000);
Cobalt.splashCont = true;
}
}
}
The program just stays at this screen: Screen EDIT: I was able to fix this by using the following code:
Invoke((MethodInvoker)delegate { MyNextForm.Show(); });
Which invokes MyNextForm.Show()
on the UI thread.