STA,MTA和OLE噩梦

时间:2009-06-30 11:31:19

标签: .net multithreading sta mta

我必须将.NET应用程序作为插件包含在另一个.NET应用程序中。插件界面要求我从模板表单继承。然后在加载插件时将表单附加到MDI中。

到目前为止一切正常,但每当我注册拖放事件时,为组合框设置自动完成模式或在其他各种情况下我得到以下异常:

  

...必须将当前线程设置为   单线程公寓(STA)模式   在进行OLE调用之前。确保   你的主要功能   STAThreadAttribute标记在其上......

主要应用程序在MTA中运行并由另一家公司开发,因此我无能为力。

我尝试在STA线程中执行导致这些异常的事情,但这也没有解决问题。

有没有人遇到过同样的情况?我有什么办法可以解决这个问题吗?

3 个答案:

答案 0 :(得分:2)

您可以尝试生成新线程并使用0调用CoInitialize(aparment线程)并在此线程中运行您的应用程序。 但是,您不会直接在此线程中更新控件,您应该为每个UI修改使用Control.Invoke。

我不知道这是否可以肯定,但你可以尝试一下。

答案 1 :(得分:1)

我最近在尝试从网络摄像头读取图像时遇到了这个问题。我最终做的是创建一个生成新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();
    }
}

答案 2 :(得分:0)

更新:该公司发布了新的STA版本。这个问题已不再适用。

相关问题