質問
私は、プラグインなどの別の.NETアプリケーションに.NETアプリケーションを含める必要があります。プラグインインターフェイスは、テンプレートフォームから継承するために私を必要とします。フォームは、プラグインがロードされMDIに取り付けられている。
すべてがこれまでに取り組んでいるが、私はドラッグのために登録して、イベントをドロップしたときに、コンボボックスまたは私は次の例外を取得し、様々な他の状況でオートコンプリートモードを設定します:
...現在のスレッドをに設定する必要があります シングルスレッドアパートメント(STA)モード 前OLE呼び出しを行うことができます。確保 あなたの主な機能は持っていること STAThreadAttributeがそれにマーク...
主な用途は、MTAで実行されていると、別の会社によって開発されたので、私はそれについてできることは何もありません。
私は、STAスレッドでこれらの例外を引き起こすことを行うことを試みたが、それはどちらかの問題を解決しませんでした。
誰もが同じような状況になっていますか?私はこの問題を解決するために何かできることはありますか?
解決 3
アップデート:企業が新しいSTAのバージョンをリリースしました。質問は、もはや関連してます。
他のヒント
あなたは新しいスレッドを生成し、それを0でのCoInitializeを呼び出す(aparmentがスレッド)と、このスレッドでアプリケーションを実行しようとすることができます。 しかし、あなたは、すべてのUIの変更のためにControl.Invokeを使用する必要があり、このスレッド内で直接コントロールを更新することができません。
これは確かに仕事に行くされている場合、私は知らないが、あなたはそれを試すことができます。
私は最近、この問題に自分自身を実行しました。私がやってしまったことは、シングルスレッド方式が実行された新しい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();
}
}