简体   繁体   中英

change code from C# windows form to class

Hi i have this one windows form but i need to modify the code so that it runs as a class not a form. The InvokeRequired and Invoke functions are stopping me from doing this. I have updated the code to show the complete class. The code constantly updates to check if a whole list of values have changed

using System;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Ets2SdkClient.Demo
{
public partial class Ets2SdkClientDemo : Form
{
    public Ets2SdkTelemetry Telemetry;

    public Ets2SdkClientDemo()
    {
        InitializeComponent();

        Telemetry = new Ets2SdkTelemetry();
        Telemetry.Data += Telemetry_Data;

        Telemetry.JobFinished += TelemetryOnJobFinished;
        Telemetry.JobStarted += TelemetryOnJobStarted;


    }

    private void TelemetryOnJobFinished(object sender, EventArgs args)
    {
        MessageBox.Show("Job finished, or at least unloaded nearby cargo destination.");
    }

    private void TelemetryOnJobStarted(object sender, EventArgs e)
    {
        MessageBox.Show("Just started job OR loaded game with active.");
    }

    private void Telemetry_Data(Ets2Telemetry data, bool updated)
    {
        try
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new TelemetryData(Telemetry_Data), new object[2] { data, updated });
                return;
            }


            // Do some magic trickery to display ALL info:
            var grps = new object[]
                   {
                       data.Drivetrain, data.Physics, data.Controls, data.Axilliary, data.Damage, data.Lights, data.Job
                   };

            foreach (var grp in grps)
            {
                // Find the right tab page:
                var grpName = grp.GetType().Name;
                if (grpName.StartsWith("_"))
                    grpName = grpName.Substring(1);



                // All properties;
                var props = grp.GetType().GetProperties().OrderBy(x => x.Name);
                var labels = new StringBuilder();
                var vals = new StringBuilder();
                foreach (var prop in props)
                {
                    labels.AppendLine(prop.Name + ":");
                    object val = prop.GetValue(grp, null);
                    if (val is float[])
                    {
                        vals.AppendLine(string.Join(", ", (val as float[]).Select(x=> x.ToString("0.000"))));
                    }
                    else
                    {
                        vals.AppendLine(val.ToString());
                    }
                }


            }
        }
        catch
        {
        }
    }
}

}

You don't need that. You can remove the entire "if" block, because all what Invoke() does here is make sure that the method executes on the same thread as the window runs on. This is no longer required if you do not have a window. However, you might need to understand thread concurrency (if your application uses multiple threads) and potentially you have to include locks to prevent such problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM