C# 使用MTA线程时使用拖放

C# 使用MTA线程时使用拖放,c#,drag-and-drop,C#,Drag And Drop,今天我遇到了一个特殊的问题,DragDrop函数在使用MTAThread时不起作用。我搜索了MSDN,用谷歌搜索了各种关键字组合 谁能给我解释一下为什么这是不允许的?有没有办法解决这个问题?我想您会遇到以下错误,这可能会稍微解释一下原因: System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be ma

今天我遇到了一个特殊的问题,DragDrop函数在使用MTAThread时不起作用。我搜索了MSDN,用谷歌搜索了各种关键字组合


谁能给我解释一下为什么这是不允许的?有没有办法解决这个问题?

我想您会遇到以下错误,这可能会稍微解释一下原因:

System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.
通常,在创建UI的任何线程上使用StatThread,以防止UI无响应/挂起。没有任何东西可以阻止您为窗口创建单独的线程(STA除外),以便您的UI响应:

 Thread thread = new Thread(() =>
  {
     Window1 w = new Window1();
     w.Show();

     System.Windows.Threading.Dispatcher.Run();
  });



  thread.SetApartmentState(ApartmentState.STA);
  thread.Start();
可以存在可以是MTA的工作线程。工作线程可以通过向调度程序传递消息与UI线程交互(在WPF的情况下)

看看这篇博文,很旧,但是提供了很多关于公寓和抽水的信息


您能解释一下为什么要使用
MTA线程吗?许多包装COM等价物的Windows窗体控件需要
STAThread
。请参阅STA required。从来都不是一个真正的问题,你只是在UI对象之间拖动。