Laurie
Laurie

Reputation:

DragDrop registration did not succeed

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

Working in VS2008 C#

Upvotes: 16

Views: 32850

Answers (11)

WiiLF
WiiLF

Reputation: 324

"Crypto Obfuscator For .NET" can also trigger this exception, in my case the DragDrop event was subscribed to (from designer), but contained no code as I commented it out much earlier on. It took a while to figure out what is was, and this was after changing every single Obfuscator config option 1 after the next.. it came down to exactly this. If you encounter this with a popular obfuscation tool, keep this in mind.

Upvotes: 1

Kosmas
Kosmas

Reputation: 385

I found this error, and the one that makes the error shown was when we have another thread calling MessageBox.Show(this, ...). However, this is not done initialized.

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

Upvotes: 1

S0und
S0und

Reputation: 377

This error also can happen, if you have async Task signature on your Main()

    [STAThread]
    static async Task Main()
    {
    }

if it's feasible change it back to void

    [STAThread]
    static void Main()
    {
    }

Upvotes: 2

Ali Sadri
Ali Sadri

Reputation: 1686

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

[STAThread]

static void Main(string[] args)

{

}

Upvotes: 4

RAVI VAGHELA
RAVI VAGHELA

Reputation: 897

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.

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

});

Thanks Happy Coding... :

Upvotes: 2

Conrad de Wet
Conrad de Wet

Reputation: 497

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
}

Upvotes: 1

zionpi
zionpi

Reputation: 2671

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

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

And InvokeHandler is a delegate like this:

private delegate void InvokeHandler();

Upvotes: 0

Sai Sherlekar
Sai Sherlekar

Reputation: 1854

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
}

Upvotes: 6

T.K.
T.K.

Reputation: 2259

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.

I just ran into this exception when trying to create and show a new form in a BackgroundWorker.DoWork method. To fix it, I wrapped the creation and showing of my new form into a method, and then called Invoke on 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.

Upvotes: 2

Charlie
Charlie

Reputation: 45072

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.

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 )

(Thread and ApartmentState are members of 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.

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.

Upvotes: 36

Graviton
Graviton

Reputation: 83286

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

Upvotes: 2

Related Questions