题
我必须包括在.NET应用程序到作为插件另一个.NET应用程序。插件接口要求我从模板的形式继承。然后形式附着在当所述插件被加载的MDI。
一切工作,到目前为止,但每当我注册拖放事件,对于ComboBox或在我得到下面的异常其他各种情况设置自动完成模式:
...当前线程必须设置为 单个线程单元(STA)模式 之前可以进行OLE调用。确保 你的主要功能有 请将STAThreadAttribute标记在它...
主要应用在MTA运行,由另一家公司开发的,所以没有什么我可以做些什么。
我想这样做会导致STA线程这些例外的事情,但它还是不解决问题。
有没有人在同样的情况?有什么我可以做的来解决这个问题?
解决方案 3
更新:该公司发布了一个新版本的STA。现在的问题是不再相关。
其他提示
您可以尝试生成新线程,并与0它调用CoInitialize(aparment线程),并在此线程运行应用程序。 然而,你将不会直接这个线程,你应该使用Control.Invoke每一个UI修改内更新控制。
我不知道这是要肯定的工作,但你可以试试。
我最近就遇到了这个问题,我尝试从网络摄像头读取的图像。我最终什么事做了创建催生了一个新的STA线程,在其上运行单线程方法的方法。
问题
private void TimerTick(object sender, EventArgs e)
{
// pause timer
this.timer.Stop();
try
{
// get next frame
UnsafeNativeMethods.SendMessage(this.captureHandle, WindowsMessageCameraGetFrame, 0, 0);
// copy frame to clipboard
UnsafeNativeMethods.SendMessage(this.captureHandle, WindowsMessageCameraCopy, 0, 0);
// notify event subscribers
if (this.ImageChanged != null)
{
IDataObject imageData = Clipboard.GetDataObject();
Image image = (Bitmap)imageData.GetData(System.Windows.Forms.DataFormats.Bitmap);
this.ImageChanged(this, new WebCamEventArgs(image.GetThumbnailImage(this.width, this.height, null, System.IntPtr.Zero)));
}
}
catch (Exception ex)
{
MessageBox.Show("Error capturing the video\r\n\n" + ex.Message);
this.Stop();
}
}
// restart timer
Application.DoEvents();
if (!this.isStopped)
{
this.timer.Start();
}
}
在溶液:移动单线程逻辑以它自己的方法,并调用从一个新的STA线程此方法
private void TimerTick(object sender, EventArgs e)
{
// pause timer
this.timer.Stop();
// start a new thread because GetVideoCapture needs to be run in single thread mode
Thread newThread = new Thread(new ThreadStart(this.GetVideoCapture));
newThread.SetApartmentState(ApartmentState.STA);
newThread.Start();
// restart timer
Application.DoEvents();
if (!this.isStopped)
{
this.timer.Start();
}
}
/// <summary>
/// Captures the next frame from the video feed.
/// This method needs to be run in single thread mode, because the use of the Clipboard (OLE) requires the STAThread attribute.
/// </summary>
private void GetVideoCapture()
{
try
{
// get next frame
UnsafeNativeMethods.SendMessage(this.captureHandle, WindowsMessageCameraGetFrame, 0, 0);
// copy frame to clipboard
UnsafeNativeMethods.SendMessage(this.captureHandle, WindowsMessageCameraCopy, 0, 0);
// notify subscribers
if (this.ImageChanged!= null)
{
IDataObject imageData = Clipboard.GetDataObject();
Image image = (Bitmap)imageData.GetData(System.Windows.Forms.DataFormats.Bitmap);
// raise the event
this.ImageChanged(this, new WebCamEventArgs(image.GetThumbnailImage(this.width, this.height, null, System.IntPtr.Zero)));
}
}
catch (Exception ex)
{
MessageBox.Show("Error capturing video.\r\n\n" + ex.Message);
this.Stop();
}
}
不隶属于 StackOverflow