We help IT Professionals succeed at work.

Real Time Programs, Databases and Threads.

AlHal2
AlHal2 asked
on
126 Views
Last Modified: 2018-10-03
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();
        }
    }
}

Open in new window

Comment
Watch Question

nociSoftware Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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.

Author

Commented:
Thanks for explaining the issues and giving meaningful examples.  Do you have any sample code or a link to some code?
Software Developer / Linux System Administrator
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
nociSoftware Engineer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
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?  If not, please tell me what is missing?
By the way, I thought this article looked interesting http://dev.ti.com/tirex/content/simplelink_academy_cc2640r2sdk_1_14_02_04/modules/rtos_concepts/rtos_concepts.html
Martyn SpencerSoftware Developer / Linux System Administrator
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
Thanks both.  I'll ask a new question when I have something more specific.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions