I'm trying to understand real time programming and found this sample program online.
Why the need for threading?
Assuming there is a genuine need for threading, what about a separate task rather than a separate thread?
What's the advantage of doing it this way rather than using a timer.
What's the best way to update a csv file with the contents of a Database table as near to real time as possible?
using System;using System.Windows.Forms;using System.Threading;using System.Diagnostics;namespace Project1{ public partial class Form1 : Form { private Thread cpuThread; private double[] cpuArray = new double[30]; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void getPerformanceCounter() { //override 3 of 6 var cpuPerfCounter = new PerformanceCounter("Processor Information", "% Processor Time", "_Total"); while (true) { cpuArray[cpuArray.Length - 1] = Math.Round(cpuPerfCounter.NextValue(), 0); Array.Copy(cpuArray, 1, cpuArray,0, cpuArray.Length - 1); if (cpuChart.IsHandleCreated) { this.Invoke((MethodInvoker)delegate { UpdateCpuChart(); }); } else { } Thread.Sleep(1000); } } private void UpdateCpuChart() { cpuChart.Series["Series1"].Points.Clear(); for (int i=0;i<cpuArray.Length-1;i++) { cpuChart.Series["Series1"].Points.Add(cpuArray[i]); } } private void button1_Click(object sender, EventArgs e) { cpuThread = new Thread(new ThreadStart(this.getPerformanceCounter)); cpuThread.IsBackground = true; cpuThread.Start(); } }}
realtime programming is about meeting deadlines all the time or 99.999+% of the time....
Every Real time task needs to be predictable in resource consumption, latency, run time.
Mostly real time programming has to deal with real world events.
Think about the ABS system in your car. It needs to monitor wheels and if needed modulate brakes to ensure they keep turning...
And the car is still stopping ..... If it starts to miss pulses from wheels due to sampling too late, the wheel might be seen as stopped...and breaks released... causing stopping problems.
In other processes a production line needs to be controlled. (many actuators, man sensors) and you still need 1000's - 100000's of products to leave the line / hour. Missing one item could mean a pile-up of (semi)products inside some machine....
Now searching databases can be a real issue in realtime systems.
How would you implement a timer? runing a loop wit some known latencies inside the loop? or waiting for a foreign event.
Using task can make use of a sleep so there is no scheduling overhead, and the wakeup would mean some data is available or some process is needed. tight looping for delays takes CPU capacity that could be needed elsewhere.
AlHal2
ASKER
Thanks for explaining the issues and giving meaningful examples. Do you have any sample code or a link to some code?
In other processes a production line needs to be controlled. (many actuators, man sensors) and you still need 1000's - 100000's of products to leave the line / hour. Missing one item could mean a pile-up of (semi)products inside some machine....
Is this example specific enough for requesting a code sample?
Not for me. It would depend on the sensors, the exact number and type, their characteristics, the physical connection method, communication protocol, timings etc. It would also depend on the hardware that will be controlling the process. As the article linked mentions, you may choose a bare metal solution (something as simple as an Arduino could be used in a deterministic fashion to control a real-time process) or a real time OS.
Personally, I am not really sure what you want to see illustrated in code. One could possibly produce some pseudo-code to describe a particular problem but I don't think it's what you are asking for. You yourself could try creating a theoretical situation and then try describing what needs to happen in pseudo code. We could then comment on what you do to help you identify issues that we feel need to be addressed.
AlHal2
ASKER
Thanks both. I'll ask a new question when I have something more specific.
Every Real time task needs to be predictable in resource consumption, latency, run time.
Mostly real time programming has to deal with real world events.
Think about the ABS system in your car. It needs to monitor wheels and if needed modulate brakes to ensure they keep turning...
And the car is still stopping ..... If it starts to miss pulses from wheels due to sampling too late, the wheel might be seen as stopped...and breaks released... causing stopping problems.
In other processes a production line needs to be controlled. (many actuators, man sensors) and you still need 1000's - 100000's of products to leave the line / hour. Missing one item could mean a pile-up of (semi)products inside some machine....
Now searching databases can be a real issue in realtime systems.
How would you implement a timer? runing a loop wit some known latencies inside the loop? or waiting for a foreign event.
Using task can make use of a sleep so there is no scheduling overhead, and the wakeup would mean some data is available or some process is needed. tight looping for delays takes CPU capacity that could be needed elsewhere.