4

I am creating a custom video player in a C# form. At present the player has a initialising and shutdown routines and a thread running in the background reading video frames and displaying them. I am fairly new to C# so I am trying to establish how best to send the start\stop\pause commands from the GUI thread to the video thread. Should I just use a state variable protected with a lock and poll this every time round my video thread; are there any other recommendations?

Thanks.

Mark Kram
  • 5,672
  • 7
  • 51
  • 70
integra753
  • 271
  • 1
  • 5
  • 19

3 Answers3

1

A polled state variable would seem the easiest solution providing your video thread loops regularly enough.

You may not even need a lock, making the state variable volatile should be sufficient in C# providing only one thread updates it. (volatile in C# has slightly different semantics than C, and should guarantee that the other thread picks up the new value)

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
  • In the end I have used a currentState and nextState varaibles with gaurds on my Start/Stop/Pause so they are only activated at the correct times. The way it is coded it does not require any locks (which is nice). – integra753 Jan 25 '12 at 14:18
0

Several ways are possible. As you are new to C# and probably tightly coupled to UI, I suggest you to use BackgroundWorker class.

http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx

You can pass your arguments using DoWorkEventArgs of DoWork event. Also with such approach and without shared objects (through threads) you can avoid using lock or synchronization

I think it could be best solution for you, but there are alternatives. You can use Asynchronouse Programming Model (APM), or even Thread/ThreadPool, or Task Parallel Library.

Should I just use a state variable protected with a lock and poll this every time round my >>video thread; are there any other recommendations?

If you have shared state like a video thread, than you should use thread synchronization. So, answer will be yes, you should use some protected variable, you can avoid locking by using just volatile, but consider to use other sync primitives. Because using volatile just ensures that you are reading/writing most actual value, but it doesn't prevents other thread from reading/writing.

Some links to choose whether to use lock(other primitives) or just a volatile:

Do I need to lock or mark as volatile when accessing a simple boolean flag in C#?

Volatile vs. Interlocked vs. lock

Community
  • 1
  • 1
Regfor
  • 8,515
  • 1
  • 38
  • 51
0

You should be able to call start/stop/pause the DirectShow filter graph without limitations. This would cause respective method calls on your source filter (for more information, see Filter States). The source filter does need to notify the background thread about the state change, if this has not been done already.

The synchronization can be implemented in the same way as in DirectShow classes, where two AutoResetEvent instances are used in the filter, one to notify background thread about new request, and one to notify calling thread about the request processing being completed.

Dmitry Shkuropatsky
  • 3,902
  • 2
  • 21
  • 13