2

I have a graph which takes long to create and get ready (5-10 sec). To aviod freezing, I decided to create my graph in thread but something weird happens. After I run my graph, while debugging; when I'm in the thread I can see video in my window. But when thread ends, video disappears. I defined everything globally about my graph. What's wrong with creating a graph in separate thread?

Thanks

Malik Çelik
  • 302
  • 1
  • 13

1 Answers1

3

It is possible to create graph in a side thread and this is a workable scenario. However this piece of information alone is insufficient to reliably explain the symptoms. As you have video, you supposedly have video renderer filter, esp. running in windowed mode. If it creates a window on this background thread, then the window expects the thread to be alive and even more, it is expected that you deliver window messages on this thread. This means, you cannot exit as soon as you run the graph.

Because of windows, including possibly internal that filters might create for their own needs, it is safer to create filter graphs on STA threads. This can be a side STA thread you create graph on, you keep it alive until you destroy the graph, you run a message loop on it in the meantime. Note there is a version of the Fitler Graph Manager suitable for this scenario:

CLSID_FilterGraphNoThread creates the Filter Graph Manager on the application's thread. If you use this CLSID, the thread that calls CoCreateInstance must have a message loop that dispatches messages; otherwise, deadlocks can occur. Also, before the application thread exits, it must release the Filter Graph Manager and all graph objects (such as filters, pins, reference clocks, and so forth).

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Do you mean I should create IGraphBuilder using CLSID_FilterGraphNoThread instead of `IGraphBuilder grapbuilder = (IGraphBuilder)new FilterGraph();`? I created FilterGraphNoFilter using its CLSID but nothing changed. What is worng with my code? Thank you very much – Malik Çelik Mar 11 '14 at 13:00
  • No, `CLSID_FilterGraphNoThread` is only a part of your solution (actually, it's even optional part). The main part is that you have to honor windows that might have been created by any filter of the graph while in paused/running state. This includes hosting thread lifetime, and message pumping. – Roman R. Mar 11 '14 at 13:34
  • So you mean that my thread should be alive all the time my graph runs? – Malik Çelik Mar 11 '14 at 14:19
  • I created my graph in thread and kept the thread alive all the time graph runs. And in a loop, I called `Application.DoEvents()` to process the messages. Everything is working with this scenario. Do you think it's good idea? – Malik Çelik Mar 16 '14 at 20:32