I came across a discussion of real time programing on this site which seemed to have some good insights. I would like to get your thoughts on my next project.

I plan to write a C# HMI for a machine which runs a short, about 30 second production cycle. There would be some setup functions that would need to look real time to the operator but the important part is the production cycle. In the production cycle, as much as possible, the controller would directly read all the inputs and control the servo motors and outputs according to parameters down loaded before the cycle. If the real time response lapses between cycles no problem.

My plan is to have a thread which reads the status from the controller and may send commands to the controller if needed, but avoids anything, like updating the screen, that might block it’s execution. A timer which gets status information from the thread then updates the on screen information and puts up error message boxes as needed. The original thread would respond to the on screen controls. I expect some sort of synchronization object will be needed to control passing data from the thread to the timer. As I understand C# the timer would share time with the original thread and could block its execution. I’m not sure if that could happen or if it would be good or bad?

What problems can you see in this plan, what should I look out for, any place I can look at other peoples work, and is there a better way?

BTW: I don’t buy many lottery tickets and I don’t drink much hard liquor so moving to the mountains and setting up a still is not yet a viable better way for me.

Thanks for your time

I agree that C# will never give true real time. I am just asking how people make the best of what C# can do.

  • Real time is one of those funny words that means different things to different people. Could you specify what it means to you? In particular, you mention this C# code is controlling servos. How detrimental is it if the servos do not receive a command for 1ms, 10ms, 100ms, 1 second, etc. Do you have to scrap the work if timing is missed, or does it just mean the work is less valuable? – Cort Ammon Jul 30 '15 at 21:22

I can tell you for certain that you will not ever achieve hard real time (and I seriously doubt you can do soft real time) with C# and the standard .NET environment.

I develop machine vision inspection systems using C# and we have offloaded all of the real-time requirements for product tracking and sensor input, etc. to a PLC (specifically a B&R real time soft-PLC)

What you will find if you attempt to do real time with .NET is that all of a sudden there will be a chunk of time that your system is simply doing "nothing". This is due to mostly garbage collection in .NET but can also be due to any number of underlying Windows tasks going on in the background (disk management, checking for updates, etc). We fondly refer to this as "Windows is licking itself". Like a dog licking its...errr....self, it isn't doing anything useful, but what it is doing is more important than what you want it to do.

  • Can you not set priority? – Ali Caglayan Mar 11 '15 at 19:28
  • Sure, you can't do proper real-time with .NET, but you can sure do "good enough" for many applications, including machine control systems. – whatsisname Apr 11 '15 at 2:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.