This problem is due to the fact that some com-classes or interfaces, for example, in DirectShowLib , should be accessible only from the same stream that was created on . Therefore, the solution to this problem is to implement ISynchronizeInvoke "System.ComponentModel.ISynchronizeInvoke".
For example, if you need to access methods in a class called Media that uses several classes or methods from DirectShowLib in multithreading mode, you need to check if you need to use the call using InvokeRequired , and if true you should access it with using the Invoke method. To demonstrate how to implement the ISynchronizeInvoke interface, here is a snippet of code that I developed some time ago in C # 2.0
public abstract class Media : ISynchronizeInvoke { //.... private readonly System.Threading.SynchronizationContext _currentContext = System.Threading.SynchronizationContext.Current; private readonly System.Threading.Thread _mainThread = System.Threading.Thread.CurrentThread; private readonly object _invokeLocker = new object(); //.... #region ISynchronizeInvoke Members public bool InvokeRequired { get { return System.Threading.Thread.CurrentThread.ManagedThreadId != this._mainThread.ManagedThreadId; } } /// <summary> /// This method is not supported! /// </summary> /// <param name="method"></param> /// <param name="args"></param> /// <returns></returns> [Obsolete("This method is not supported!", true)] public IAsyncResult BeginInvoke(Delegate method, object[] args) { throw new NotSupportedException("The method or operation is not implemented."); } /// <summary> /// This method is not supported! /// </summary> /// <param name="method"></param> /// <param name="args"></param> /// <returns></returns> [Obsolete("This method is not supported!", true)] public object EndInvoke(IAsyncResult result) { throw new NotSupportedException("The method or operation is not implemented."); } public object Invoke(Delegate method, object[] args) { if (method == null) { throw new ArgumentNullException("method"); } lock (_invokeLocker) { object objectToGet = null; SendOrPostCallback invoker = new SendOrPostCallback( delegate(object data) { objectToGet = method.DynamicInvoke(args); }); _currentContext.Send(new SendOrPostCallback(invoker), method.Target); return objectToGet; } } public object Invoke(Delegate method) { return Invoke(method, null); } #endregion//ISynchronizeInvoke Members }
source share