Home > Software design >  Why it doesn't work, The Console program converts to Windows Form C #
Why it doesn't work, The Console program converts to Windows Form C #

Time:12-18

I have a program to read telemetry from the game after UDP, in the console version it works perfectly. I want to make a version of Windows Form and I have no idea how to force it to work, theoretically I think it should work. Sorry for maybe a trivial question, I'm a beginner.

Console Program:

namespace F12021
{
    class Program
    {
        static void Main(string[] args)
        {
            F12020TelemetryClient client = new F12020TelemetryClient(20777);
            client.OnCarTelemetryDataReceive  = TelemetryUpdate;

            client.OnConnectStatusChanged  = OnConnectStatusChange;
            Console.CursorVisible = false;
            Console.Read();
        }

        private static void OnConnectStatusChange(bool connected)
        {
            if (!connected)
            {
                Console.Clear();
                Console.WriteLine("Listening for F1 2020...");
            }
        }

        private static void TelemetryUpdate(PacketCarTelemetryData packet)
        {
            int playerIndex = packet.Header.playerCarIndex;
            CarTelemetryData playerData = packet.carTelemetryData[playerIndex];
            Console.SetCursorPosition(0, 0);

            WriteLine($"Throttle: {playerData.throttle}");
        }

        private static void WriteLine(string input)
        {
            Console.Write(input.PadRight(Console.WindowWidth, ' '));
        }
    }
}

Windows Form:

namespace F1_2021
{
    public partial class Form1 : Form
    {
       
       
        public Form1()
        {
            
            InitializeComponent();
        }
        

        private void Form1_Load(object sender, EventArgs e)
        {
            F12020TelemetryClient client = new F12020TelemetryClient(20777);
            client.OnCarTelemetryDataReceive  = TelemetryUpdate;
            client.OnConnectStatusChanged  = OnConnectStatusChange;
        }

        private void OnConnectStatusChange(bool connected)
        {
            if (!connected) { label1.Text = "Not conected wait....."; }
            else {  label1.Text = "Conected"; }
        }

        private void TelemetryUpdate(PacketCarTelemetryData packet)
        {
             
            int playerIndex = packet.Header.playerCarIndex;
            CarTelemetryData playerData = packet.carTelemetryData[playerIndex];
            label2.Text = (playerData.throttle).ToString() ;

        }
    }
}

What do I not understand?

CodePudding user response:

In your console application, the F12020TelemetryClient instance is kept alive for as long as the application is running.

In your Windows Forms application, it's a local variable within the Form_Load method. As soon as that method ends, it's eligible for Garbage Collection. Make it a field of your class instead.

You'll also need to check whether the events are raised on the UI thread; if not, you'll need to marshal the calls to the UI thread before you can update any controls.

public partial class Form1 : Form
{
    private F12020TelemetryClient _client;
    
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        _client = new F12020TelemetryClient(20777);
        _client.OnCarTelemetryDataReceive  = TelemetryUpdate;
        _client.OnConnectStatusChanged  = OnConnectStatusChange;
    }

    private void OnConnectStatusChange(bool connected)
    {
        if (InvokeRequired)
        {
            BeginInvoke((Action<bool>)OnConnectStatusChange, connected);
        }
        else
        {
            if (!connected) { label1.Text = "Not conected wait....."; }
            else {  label1.Text = "Conected"; }
        }
    }

    private void TelemetryUpdate(PacketCarTelemetryData packet)
    {
        if (InvokeRequired)
        {
            BeginInvoke((Action<PacketCarTelemetryData>)TelemetryUpdate, packet);
        }
        else
        {
            int playerIndex = packet.Header.playerCarIndex;
            CarTelemetryData playerData = packet.carTelemetryData[playerIndex];
            label2.Text = (playerData.throttle).ToString() ;
        }
    }
}

CodePudding user response:

try to move client to the top

 public partial class Form1 : Form
{
 
 F12020TelemetryClient client = new F12020TelemetryClient(20777);

 public Form1()
....

CodePudding user response:

Thank you everything is already working correctly.

namespace F1_2021
{
    public partial class Form1 : Form
    {
       
        F12020TelemetryClient client = new F12020TelemetryClient(20777);

        public Form1()
        {
            
            InitializeComponent();
        }
        

        private void Form1_Load(object sender, EventArgs e)
        {
            client.OnCarTelemetryDataReceive  = TelemetryUpdate;
            client.OnConnectStatusChanged  = OnConnectStatusChange;
          
        }
 
        private void OnConnectStatusChange(bool connected)
        {
            label1.Invoke(new Action(delegate ()
            {
                if (!connected) { label1.Text = "Not conected wait....."; }
                else { label1.Text = "Conected"; }
            }));
        }

        private void TelemetryUpdate(PacketCarTelemetryData packet)
        {
             
            int playerIndex = packet.Header.playerCarIndex;
            CarTelemetryData playerData = packet.carTelemetryData[playerIndex];
              label2.Invoke(new Action(delegate ()
              {
                label2.Text = (playerData.throttle).ToString();
              }));       

        }
    }
}
  • Related