Click here to Skip to main content
15,867,999 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi so I am new to events, sockets, and event handling in C#. I would like to do some processing with some values in the event (in this case temp) which constantly updates and displays according to the code below.

I want to create functions and while loops that utilize the temp of the of the event in a way that updates in the same way temp does.

For example, I want to be able to do an average of temp over time, so i would need to sum the temps and then divide by the number of readings. The code is below and I would appreciate any sample code and/or feedback!

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using tyllibrary;
using System.IO;


namespace SKTtest
{
    class Program
    {
        static void Main(string[] args)
        {

            TYLSocket socket = new TYLSocket();
            socket.Connect("188.2.2.112", 2800);

            int Summation1 = 0;

            socket.eventPool.Subscribe("flight", new EventHandler<flightArgs>((x, y) => Console.WriteLine(y.STR1.temp + " " + y.STR1.alt)));
            socket.SubscribeEquip("gulfstream");
     

            Console.ReadKey();
        }
    }
}
Posted
Updated 10-Aug-21 21:55pm

1 solution

Seems simple enough:
C#
int readingCount = 0;
decimal sumOfTemperatures = 0;
socket.eventPool.Subscribe("flight", (x, y) =>
{
    sumOfTemperature += y.STR1.temp;
    readingCount++;
    
    Console.WriteLine("{0} {1}", y.STR1.temp, y.STR1.alt);
    Console.WriteLine("Average temperature: {0}", sumOfTemperature / readingCount);
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900