日期:2014-05-18  浏览次数:21129 次

C# DirectShow开发 资源释放的问题
我用如下方法播放视频,每次播放新文件时都先调用Init,再调用Open
播放没有问题,资源占用也都正常,只是每次播放一个新文件资源监视器都会发现程序多了一个线程,播放几十个文件以后会多出几十个线程,但是感觉这些线程都没有资源消耗,内存、Cpu都正常
请问要怎么才能把DirectShow的资源清理干净?

C# code

        public void Init()
        {
            if (_graph != null)
            {
                ReleaseGraph();
                Marshal.ReleaseComObject(_graph);

                _graph = null;
                GC.Collect();
            }

            _graph = new FilterGraph() as IFilterGraph;
            if (_graph != null) _graph.AddFilter(CreateRenderer() as IBaseFilter, "Video Mixing Renderer 9");

            State = MediaState.Close;
        }

        public void Open(string filename)
        {
            var mediaControl = _graph as IMediaControl;
            if (mediaControl != null)
            {
                mediaControl.RenderFile(filename);

                State = MediaState.Open;
            }
        }

        private void ReleaseGraph()
        {
            IEnumFilters filters = null;
            _graph.EnumFilters(out filters);
            IBaseFilter[] filter = new IBaseFilter[1];
            while (filters.Next(1, filter, IntPtr.Zero) == 0)
            {
                _graph.RemoveFilter(filter[0]);
            }
        }




------解决方案--------------------
看到COM、线程字样,脑袋顿时大了1倍……