1

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?

Community
  • 1
  • 1
Jack
  • 16,276
  • 55
  • 159
  • 284
  • Can you post the stack trace for the exception? I am guessing its blowing up at this.Close(); in method backgroundWorker1_RunWorkerCompleted – Keith Neuse Jan 19 '15 at 03:07
  • @KeithNeuse: Even if I remove it, still get the same exception. – Jack Jan 19 '15 at 03:18
  • Where are you calling SetAcceptDrops method at? I am guessing the above code is sample code and not the code you are working with. – Keith Neuse Jan 19 '15 at 03:25
  • It's my actual code, I'm not calling `SetAcceptDrops()` (at least, not directly) but I solved it: In `DoWork()` using `this` instead of `form`: `if (this.InvokeRequired) this.BeginInvoke(new Action(() => form.Show()));` – Jack Jan 19 '15 at 03:27
  • I see that now, and I dug this up on StackOverFlow: http://stackoverflow.com/questions/3185861/system-invalidoperationexception-in-vs-2005 – Keith Neuse Jan 19 '15 at 03:29
  • Yes, you are doing this very, very wrong. This exception is but a small part of the enormous trouble you are getting yourself into. The .NET Framework has excellent support for splash screens, and single-instance apps, don't reinvent that wheel. – Hans Passant Jan 19 '15 at 08:28
  • @HansPassant: Thanks for the comment. You're right. Right now I'm getting another exception which I have no idea from where it come from. And what is scareing is the fact it doesn't happend in every run. Sometimes it does works normally. So, how should I implement them? Which excellent .NET support are you refering to? I did some (maybe poor) search and didn't found anything .NET's which would help me but a lot of apart implementations, like the mine. – Jack Jan 19 '15 at 20:45
  • @HansPassant: To splash screen, I'm using your approach from http://stackoverflow.com/questions/392864/splash-screen-waiting-until-thread-finishes but what about the one better to single instance of application? – Jack Jan 19 '15 at 21:25
  • Set the MyApp.IsSingleInstance property to true, add a constructor. – Hans Passant Jan 19 '15 at 21:58

0 Answers0