WndProc()中的操作异常无效

时间:2015-01-19 02:41:45

标签: c# winforms backgroundworker

我正在尝试在我的应用程序上实现启动画面,但是我收到了这个错误:

A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

来自这个功能:

(如果问题,它是单个实例应用程序实现的一部分,请从this获取)

protected override void WndProc(ref Message m)
        {
            if (m.Msg == NativeMethods.WM_SHOWME)
            {
                ShowMe();
            }

            base.WndProc(ref m);
        }

我将program.cs修改为:

  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new SplashScreen(new Form1()))

这里是SplashScreen课程:

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();
        }
    }

更新:堆栈跟踪:

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)

(注意:错误信息不是用英语写的,而是用我的母语写的,所以我自己翻译了。很抱歉任何错误。)

为什么我会收到这个错误,我会解决这个问题吗?

0 个答案:

没有答案