DragDrop registration did not succeed

前端 未结 9 1818
时光取名叫无心
时光取名叫无心 2021-02-07 08:14

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

What does this except

相关标签:
9条回答
  • 2021-02-07 08:29

    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
    }
    
    0 讨论(0)
  • 2021-02-07 08:33
    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
    }
    
    0 讨论(0)
  • 2021-02-07 08:40

    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.

    0 讨论(0)
  • 2021-02-07 08:40

    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();
    
    0 讨论(0)
  • 2021-02-07 08:44

    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.

    0 讨论(0)
  • 2021-02-07 08:45

    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.

    0 讨论(0)
提交回复
热议问题