C# DragDrop注册没有成功

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/135803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:02:56  来源:igfitidea点击:

DragDrop registration did not succeed

提问by

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

System.InvalidOperationException: DragDrop 注册未成功。---> 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);

Working in VS2008 C#

在 VS2008 C# 中工作

回答by Charlie

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.

此异常意味着拥有面板(正在添加的面板)的线程已使用 MTA 线程模型进行初始化。拖放系统要求调用线程使用 STA 线程模型(特别是它要求通过 OleInitialize 初始化 COM)。线程模型是 COM(.NET 平台的前身)的不幸遗迹。

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):

如果您[STAThread]的 Main 函数具有该属性,则主程序线程应该已经是 STA。那么,最可能的解释是此异常发生在不同的线程上。发生异常时查看 Visual Studio 中的 Threads 窗口(Debug | Windows | Threads),看看您是否在主线程以外的线程上。如果是,解决方案可能就像为该新线程设置线程模型一样简单,您可以执行以下操作(将此代码添加到正在创建控件的线程中):

Thread.CurrentThread.SetApartmentState( ApartmentState.STA )

Thread.CurrentThread.SetApartmentState( ApartmentState.STA )

(Threadand ApartmentStateare members of System.Threading)

Thread并且ApartmentState是 的成员System.Threading

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.

该代码需要在您实际启动新线程之前发生。正如@Tomer 所指出的,您还可以使用[STAThread]属性以声明方式指定这一点。

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.

如果您发现异常发生在主线程上,请回帖告诉我们,也许我们可以提供更多帮助。异常发生时的堆栈跟踪可能有助于跟踪问题。

回答by Graviton

I'm not sure whether you have solved this problem or not. I just encountered this problem and I fixed it by deleting my bindirectory.

我不确定你是否已经解决了这个问题。我刚遇到这个问题,我通过删除我的bin目录来修复它。

回答by T.K.

Yes, I realize this question was asked 2 and a half years ago. I hit this exception and did some reading on it. I corrected it, but didn't see my solution anywhere, so I thought I'd post it somewhere someone else could read.

是的,我意识到这个问题是在两年半前提出的。我遇到了这个异常并对其进行了一些阅读。我更正了它,但没有在任何地方看到我的解决方案,所以我想我会把它贴在其他人可以阅读的地方。

One possibility for this happening with [STAThread]marked on the Main()is if you're running this on a thread other than the one you started on.

对于这种情况的发生与一种可能性[STAThread]上标明的Main()是,如果你在比你开始对另外一个线程运行此。

I just ran into this exception when trying to create and show a new form in a BackgroundWorker.DoWorkmethod. To fix it, I wrapped the creation and showing of my new form into a method, and then called Invokeon that method so that it fired on the UI thread. This worked because the UI thread started from the Main()method with [STAThread]marked, as other answers here explained.

我在尝试在方法中创建和显示新表单时遇到了这个异常BackgroundWorker.DoWork。为了解决这个问题,我将新表单的创建和显示包装到一个方法中,然后调用Invoke该方法,使其在 UI 线程上触发。这是有效的,因为 UI 线程从Main()带有[STAThread]标记的方法开始,正如这里的其他答案所解释的那样。

回答by Sai Sherlekar

function abc
{
            Thread t = new Thread(new ThreadStart(xyz));
            t.SetApartmentState(ApartmentState.STA);
            t.Start( );    
}
function xyz
{
 the code of Windows form..or whatever which is causing the error
}

回答by zionpi

I have encountered this situation recently,[STAThreadAttribute]is in my case,and i solved this problem by using Invokemethod,it might be helpful for you guys,so I share with a little code snippet:

我最近遇到了这种情况,[STAThreadAttribute]就我而言,我通过使用Invoke方法解决了这个问题,它可能对你们有帮助,所以我分享一个小代码片段:

this.Invoke(new InvokeHandler(delegate() 
{
//Your method here!
}));

And InvokeHandler is a delegate like this:

InvokeHandler 是这样的委托:

private delegate void InvokeHandler();

回答by Conrad de Wet

By far the easiest way is:

到目前为止,最简单的方法是:

private void DoSomethingOnGui()
{
    if (this.InvokeRequired)
    {
        this.Invoke((MethodInvoker)delegate
        {
            Safe_DoSomethingOnGui();
        });
    }
    else
    {
        Safe_DoSomethingOnGui();
    }
}
private void Safe_DoSomethingOnGui()
{
    // Do whatever you want with the GUI
}

You can even pass things along no problem:

你甚至可以毫无问题地传递东西:

private void DoSomethingOnGui(object o)
{
    if (this.InvokeRequired)
    {
        this.Invoke((MethodInvoker)delegate
        {
            Safe_DoSomethingOnGui(o);
        });
    }
    else
    {
        Safe_DoSomethingOnGui(o);
    }
}
private void Safe_DoSomethingOnGui(object o)
{
    // Do whatever you want with the GUI and o
}

回答by Kosmas

I found this error, and I see that the one that making it shown is when using another thread calling MessageBox.Show(this, ...), where this is not done initialized.

我发现了这个错误,我看到显示它的那个是在使用另一个线程调用时MessageBox.Show(this, ...),这没有完成初始化。

We need to remove the owner of the message box to remove the error.

我们需要删除消息框的所有者才能消除错误。

回答by RAVI VAGHELA

I solved this error by using below code...I were using Background Worker and trying to access UI while background worker..that is why getting error - DragDrop registration did not succeed. We cannot access UI from the code running in background worker or in thread.

我通过使用下面的代码解决了这个错误......我正在使用后台工作人员并尝试在后台工作人员时访问用户界面......这就是为什么出现错误 - DragDrop 注册没有成功。我们无法从后台工作程序或线程中运行的代码访问 UI。

BeginInvoke((MethodInvoker)delegate
{
//write your code here...

});

Thanks Happy Coding... :

谢谢快乐编码...:

回答by Ali Sadri

Add the STAThreadAttribute attribute on the Main method. This attribute is required if your program access OLE related functions, like Clipboard class does.

在 Main 方法上添加 STAThreadAttribute 属性。如果您的程序访问与 OLE 相关的函数,则此属性是必需的,例如 Clipboard 类。

[STAThread]

[STAThread]

static void Main(string[] args)

static void Main(string[] args)

{

{

}

}