的DragDrop注册没有成功 [英] DragDrop registration did not succeed

查看:887
本文介绍了的DragDrop注册没有成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.InvalidOperationException:注册的DragDrop没有
成功。 ---> System.Threading.ThreadStateException:

System.InvalidOperationException: DragDrop registration did not succeed. ---> System.Threading.ThreadStateException:

这是什么意思例外?我明白了这行试图将面板在运行时添加到面板...

What does this exception mean? I get it at this line trying to add a panel to a panel at runtime...

splitReport.Panel1.Controls.Add(ChartPanel);

在VS2008 C#工作

Working in VS2008 C#

推荐答案

这异常是指拥有该面板(添加的面板)线程一直使用MTA线程模型初始化。拖/放系统需要调用线程使用STA线程模型(特别是它需要通过COM来OleInitialize初始化)。线程模型是COM,NET平台的前任不幸的痕迹。

This exception means that the thread that owns the Panel (the Panel being added) has been initialized using the MTA threading model. The drag/drop system requires that the calling thread use the STA thread model (particularly it requires that COM be initialized via OleInitialize). Threading models are an unfortunate vestige of COM, a predecessor of the .NET platform.

如果您有 [STAThread] 你的主要功能属性,那么主程序线程应该已经STA。最可能的解释,那么,这个异常是在不同的线程发生。看看在Visual Studio中的线程窗口(调试|窗口|主题)发生异常时,看看你是比主线程的线程上。如果你是,解决的办法很可能是因为设置线程模型为新的线程,它可以做如下简单(将此代码添加到正在创建控件的线程):

If you have the [STAThread] attribute on your Main function, then the main program thread should already be STA. The most likely explanation, then, is that this exception is happening on a different thread. Look at the Threads window in Visual Studio (Debug | Windows | Threads) when the exception occurs and see if you are on a thread other than the main thread. If you are, the solution is probably as simple as setting the thread model for that new thread, which you can do as follows (add this code to the thread where the control is being created):

Thread.CurrentThread.SetApartmentState(ApartmentState.STA)

的ApartmentState 的System.Threading

这代码需要在你真正开始新线程的情况发生。正如@Tomer指出的那样,你还可以指定这一声明使用 [STAThread] 属性。

That code will need to happen before you actually start the new thread. As noted by @Tomer, you can also specify this declaratively using the [STAThread] attribute.

如果你发现该异常是在主线程上发生的事情,回来后让我们知道,也许我们可以帮助更多。在异常时堆栈跟踪可能有助于追查问题。

If you find that the exception is happening on the main thread, post back and let us know, and maybe we can help more. A stack trace at the time of the exception may help track down the problem.

这篇关于的DragDrop注册没有成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆