c# - realtime - web application real time data

using C# for real time applications (4)

Can C# be used for developing a real-time application that involves taking input from web cam continuously and processing the input?

Absolutely. The key will be to avoid garbage collection and memory management as much as possible. Try to avoid new-ing objects as much as possible, using buffers or object pools when you can.

I've used C# to create multiple realtime, high speed, machine vision applications that run 24/7 and have moving machinery dependent on the application. If something goes wrong in the software, something immediately and visibly goes wrong in the real world.

I've found that C#/.Net provide pretty good functionality for doing so. As others have said, definitely stay on top of garbage collection. Break up to processing into several logical steps, and have separate threads working each. I've found the Producer Consumer programming model to work well for this, perhaps ConcurrentQueue for starters.

You could start with something like:

  • Thread 1 captures the camera image, converts it to some format, and puts it into an ImageQueue
  • Thread 2 consumes from the ImageQueue, processing the image and comes up with a data object that is put onto a ProcessedQueue
  • Thread 3 consumes from the ProcessedQueue and does something interesting with the results.

If Thread 2 takes too long, Threads 1 and 3 are still chugging along. If you have a multicore processor you'll be throwing more hardware at the math. You could also use several threads in place of any thread that I wrote above, although you'd have to take care of ordering the results manually.


After reading other peoples answers, you could probably argue my definition of "realtime". In my case, the computer produces targets that it sends to motion controllers which do the actual realtime motion. The motion controllers provide their own safety layers for things like timing, max/min ranges, smooth accel/decelerations and safety sensors. These controllers read sensors across an entire factory with a cycle time of less than 1ms.

You cannot use any main stream garbage collected language for “hard real-time systems”, as the garbage collect will sometimes stop the system responding in a defined time. Avoiding allocating object can help, however you need a way to prove you are not creating any garbage and that the garbage collector will not kick in.

However most “real time” systems don’t in fact need to always respond within a hard time limit, so it all comes down do what you mean by “real time”.

Even when parts of the system needs to be “hard real time” often other large parts of the system like the UI don’t.

(I think your app needs to be fast rather than “real time”, if 1 frame is lost every 100 years how many people will get killed?)

  • Of course, someone has even developed a library to do that: AForge.NET
  • As with any real-time application and not just C#, you'll have to manage the buffers well as @David suggested.
  • Not only that, there're also the XNA Framework (for things like 3D games) and you can program DirectX using C# as well which are very real-time.
  • And did you know that, if you want, you can do pointer manipulations in C# too?