I'm trying to implement a Splash Screen on my application but I'm getting this error:
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
from this function:
(if matter, it's part of a single instance application implmentation, take from this)
protected override void WndProc(ref Message m)
{
if (m.Msg == NativeMethods.WM_SHOWME)
{
ShowMe();
}
base.WndProc(ref m);
}
I modified my program.cs to like this:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new SplashScreen(new Form1()))
And here's SplashScreen
class:
public partial class SplashScreen : Form
{
Form1 mainForm;
public SplashScreen(Form1 mainForm)
{
InitializeComponent();
this.mainForm = mainForm;
backgroundWorker1.RunWorkerAsync();
}
private void SplashScreen_Load(object sender, EventArgs e)
{
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (mainForm.InvokeRequired)
mainForm.BeginInvoke(new Action(initMainForm));
else
initMainForm();
//mainForm.Show();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.Close();
}
void initMainForm()
{
mainForm.Show();
}
}
UPDATE: stack trace:
System.InvalidOperationException: DragDrop registration did not succeed.
---> System.Threading.ThreadStateException: The current thread must be defined in STA mode (single thread apartment) before OLE calls can be made. Check if the Main function has STAThreadAttribute marked.
em System.Windows.Forms.Control.SetAcceptDrops(Boolean accept)
(note: the error message isn't written in english but in my native language so I translated it myself. Sorry for any error.)
Why am I getting that error and hwo do I fix this?