数据未向图表添加点数,C#

时间:2016-10-21 18:10:04

标签: c# winforms windows-forms-designer

static void Main(string[] args)
{
    ThreadStart dataRead = new ThreadStart(readInData);
    ThreadStart appStart = new ThreadStart(startApp);
    Thread appThread = new Thread(appStart);
    Thread childThread = new Thread(dataRead);

    appThread.Start();
    childThread.Start(); 
}

public static void startApp()
{
    Application.Run(new Form1());
}

public static void readInData()
{
    Form1 cls = new Form1();
    clock.Start();

    try
    {
        mySerialPort.PortName = "COM3";
        mySerialPort.BaudRate = 9600;
        mySerialPort.DataBits = 8;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.Open();
        Console.WriteLine("Opened");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Sorry! " + ex);
    }

    while (Continue)
    {
        string message = mySerialPort.ReadLine();

        Console.WriteLine(message);
        if (message == "Altitude\r")
        {
            origAlt = mySerialPort.ReadLine();
            Console.WriteLine(origAlt);
            Continue = false; 
        }
    }

    Continue = true;
    while (Continue)
    {
        TimeSpan timePassed = clock.Elapsed;
        string message = mySerialPort.ReadLine();

        if (stringComparer.Equals("Next\r", message))
        {
            Continue = true;
        }
        Console.WriteLine(message);
        if (message == "Altitude\r")
        {
            altitude = mySerialPort.ReadLine();
            cls.setChartData(altitude, timePassed.Seconds);

            Console.WriteLine(altitude);
        }
        else if (message == "Temperature\r")
        {
            temperature = mySerialPort.ReadLine();
            cls.setGuageData(temperature);

            Console.WriteLine(temperature);
        }
    }
}

public void setChartData(string alt, int seconds)
{
    chart1.Series["AltitudeChart"].Points.AddXY(seconds, alt);
    chart1.Update();
}

上面的线程调用的函数setChartData()正在正确运行,当我放置断点时,它清楚地到达了这一点,你可以通过intellisense看到过去的变量有值。出于某种原因,这一点没有被添加到图表中,或者至少我无法看到是否添加了该点。我该如何解决这个问题?

在我开始使用多线程之前,此命令有效,将该点明显添加到图表中。多线程怎么能打破这个?

0 个答案:

没有答案