2011-06-09 89 views
2

我使用directshowlib-2005在c#中制作了一个电视播放器。 现在我做了一个搜索可用频道的方法。DirectShow过滤器访问线程

我想让这个方法在不同的线程中运行,所以我的GUI不会冻结,但是当我尝试在方法中设置通道时出现错误。它无法在我的图表中找到IAMTVTuner接口,尽管我知道它在那里。

如果我不使用不同的线程,该方法工作得很好(但我的GUI冻结了一会儿)

我知道它是做什么的公寓,但有什么办法,我可以ACCES该接口在不同的线程中,然后线程创建我的图形?

+0

什么是** **确切的错误?它是来自应用程序本身的异常还是某种“状态消息”? – Kiril 2011-06-09 19:58:39

回答

1

这个问题是因为像DirectShowLib中的一些com类或接口应该只是从访问,它与上创建的线程相同。 所以这个问题的解决方案是实现ISynchronizeInvoke“System.ComponentModel.ISynchronizeInvoke”。

例如,如果你需要访问一个名为Media在内部使用从DirectshowLib某些类或方法在多线程模式类的方法,你必须检查是否需要使用InvokeRequired调用,如果属实,您必须通过访问Invoke方法。 为了演示如何实现此ISynchronizeInvoke接口从代码片段,我开发前一段时间在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 

} 
+1

@赫尔曼:你试过这个吗?它解决了你的问题吗? – 2011-06-22 18:16:02