C#WinForms试图保持时间同步

C#WinForms试图保持时间同步,c#,multithreading,winforms,timer,mschart,C#,Multithreading,Winforms,Timer,Mschart,您好,我正在尝试每秒更新我的图表,所有图表应始终同时更新。为了更好地理解,我将包括一张图片,但首先我将解释实际发生的情况 所以我要ping发送请求,每次有结果时,它都会将其写入一个名为file的数据点数组中。一切正常,一切正常 同时,两个计时器正在运行,一个计时器调用一个准备数据的方法(假设在特定时间数组中找不到数据->它应该只设置值0)。准备好的数据不在缓冲区中 第二个计时器正在更新UI并从tempData中读取数据,但这并没有按照预期或希望的那样工作 计时器: myTimer.Interva

您好,我正在尝试每秒更新我的图表,所有图表应始终同时更新。为了更好地理解,我将包括一张图片,但首先我将解释实际发生的情况

所以我要ping发送请求,每次有结果时,它都会将其写入一个名为file的数据点数组中。一切正常,一切正常

同时,两个计时器正在运行,一个计时器调用一个准备数据的方法(假设在特定时间数组中找不到数据->它应该只设置值0)。准备好的数据不在缓冲区中

第二个计时器正在更新UI并从tempData中读取数据,但这并没有按照预期或希望的那样工作

计时器:

myTimer.Interval = 1000;
myTimer.Tick += FileReadFunction;

aTimer.Elapsed += new System.Timers.ElapsedEventHandler(prepData);
aTimer.Interval = 1000;
单击启动计时器的按钮:

private void _tbStartAll_Click(object sender, EventArgs e)
        {
            lock (_hosts)
            {
                foreach (HostPinger hp in _hosts)
                    hp.Start();

                myTimer.Start();
                aTimer.Enabled = true;
            }
        }
 public void prepData(object objectInfo, EventArgs e)
        {
            foreach (NetPinger.source.AddGraph b in graphList)
            {
                b.prepareData();
            }
        }
public void prepareData()
        {
            double unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

            for (double i = unixTimestamp; unixTimestamp - graphSizing < i; i--)
            {
                bool exists;
                try
                {
                    exists = Array.Exists(file, element => element.XValue == i);
                    exists = true;
                }
                catch
                {
                    exists = false;
                }
                try
                {
                    if (exists == false)
                    {
                        TempBuffer = TempBuffer.Skip(1).Concat(new DataPoint[] { new DataPoint(i, 0) }).ToArray();
                    }
                    else
                    {
                        DataPoint point = Array.Find(file, element => element.XValue == i);
                        TempBuffer = TempBuffer.Skip(1).Concat(new DataPoint[] { (point) }).ToArray();
                    }
                }
                catch (Exception ex)
                {
                    //just for debugging...
                }
            }
        }
private void FileReadFunction(object objectInfo, EventArgs e)
        {
            foreach (NetPinger.source.AddGraph b in graphList)
            {
                b.fileRead();
            }
        }
 public void fileRead()
        {
            //double unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            chart_holder.Series[0].Points.Clear();
            foreach (DataPoint a in TempBuffer)
            {
                chart_holder.Series[0].Points.Add(a);
            }
}
在表单类中准备数据的方法:

private void _tbStartAll_Click(object sender, EventArgs e)
        {
            lock (_hosts)
            {
                foreach (HostPinger hp in _hosts)
                    hp.Start();

                myTimer.Start();
                aTimer.Enabled = true;
            }
        }
 public void prepData(object objectInfo, EventArgs e)
        {
            foreach (NetPinger.source.AddGraph b in graphList)
            {
                b.prepareData();
            }
        }
public void prepareData()
        {
            double unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

            for (double i = unixTimestamp; unixTimestamp - graphSizing < i; i--)
            {
                bool exists;
                try
                {
                    exists = Array.Exists(file, element => element.XValue == i);
                    exists = true;
                }
                catch
                {
                    exists = false;
                }
                try
                {
                    if (exists == false)
                    {
                        TempBuffer = TempBuffer.Skip(1).Concat(new DataPoint[] { new DataPoint(i, 0) }).ToArray();
                    }
                    else
                    {
                        DataPoint point = Array.Find(file, element => element.XValue == i);
                        TempBuffer = TempBuffer.Skip(1).Concat(new DataPoint[] { (point) }).ToArray();
                    }
                }
                catch (Exception ex)
                {
                    //just for debugging...
                }
            }
        }
private void FileReadFunction(object objectInfo, EventArgs e)
        {
            foreach (NetPinger.source.AddGraph b in graphList)
            {
                b.fileRead();
            }
        }
 public void fileRead()
        {
            //double unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            chart_holder.Series[0].Points.Clear();
            foreach (DataPoint a in TempBuffer)
            {
                chart_holder.Series[0].Points.Add(a);
            }
}
准备数据方法:

private void _tbStartAll_Click(object sender, EventArgs e)
        {
            lock (_hosts)
            {
                foreach (HostPinger hp in _hosts)
                    hp.Start();

                myTimer.Start();
                aTimer.Enabled = true;
            }
        }
 public void prepData(object objectInfo, EventArgs e)
        {
            foreach (NetPinger.source.AddGraph b in graphList)
            {
                b.prepareData();
            }
        }
public void prepareData()
        {
            double unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

            for (double i = unixTimestamp; unixTimestamp - graphSizing < i; i--)
            {
                bool exists;
                try
                {
                    exists = Array.Exists(file, element => element.XValue == i);
                    exists = true;
                }
                catch
                {
                    exists = false;
                }
                try
                {
                    if (exists == false)
                    {
                        TempBuffer = TempBuffer.Skip(1).Concat(new DataPoint[] { new DataPoint(i, 0) }).ToArray();
                    }
                    else
                    {
                        DataPoint point = Array.Find(file, element => element.XValue == i);
                        TempBuffer = TempBuffer.Skip(1).Concat(new DataPoint[] { (point) }).ToArray();
                    }
                }
                catch (Exception ex)
                {
                    //just for debugging...
                }
            }
        }
private void FileReadFunction(object objectInfo, EventArgs e)
        {
            foreach (NetPinger.source.AddGraph b in graphList)
            {
                b.fileRead();
            }
        }
 public void fileRead()
        {
            //double unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            chart_holder.Series[0].Points.Clear();
            foreach (DataPoint a in TempBuffer)
            {
                chart_holder.Series[0].Points.Add(a);
            }
}
方法文件读取/更新图表:

private void _tbStartAll_Click(object sender, EventArgs e)
        {
            lock (_hosts)
            {
                foreach (HostPinger hp in _hosts)
                    hp.Start();

                myTimer.Start();
                aTimer.Enabled = true;
            }
        }
 public void prepData(object objectInfo, EventArgs e)
        {
            foreach (NetPinger.source.AddGraph b in graphList)
            {
                b.prepareData();
            }
        }
public void prepareData()
        {
            double unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

            for (double i = unixTimestamp; unixTimestamp - graphSizing < i; i--)
            {
                bool exists;
                try
                {
                    exists = Array.Exists(file, element => element.XValue == i);
                    exists = true;
                }
                catch
                {
                    exists = false;
                }
                try
                {
                    if (exists == false)
                    {
                        TempBuffer = TempBuffer.Skip(1).Concat(new DataPoint[] { new DataPoint(i, 0) }).ToArray();
                    }
                    else
                    {
                        DataPoint point = Array.Find(file, element => element.XValue == i);
                        TempBuffer = TempBuffer.Skip(1).Concat(new DataPoint[] { (point) }).ToArray();
                    }
                }
                catch (Exception ex)
                {
                    //just for debugging...
                }
            }
        }
private void FileReadFunction(object objectInfo, EventArgs e)
        {
            foreach (NetPinger.source.AddGraph b in graphList)
            {
                b.fileRead();
            }
        }
 public void fileRead()
        {
            //double unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            chart_holder.Series[0].Points.Clear();
            foreach (DataPoint a in TempBuffer)
            {
                chart_holder.Series[0].Points.Add(a);
            }
}
时间同步的图像示例:

我有点不明白为什么它不起作用,是因为一个线程比另一个线程快吗?或者原因是什么,我该如何解决?我非常感谢你的帮助


问候C.User

我通过稍微更改代码解决了这个问题。为了保持同步,我先准备数据,然后再显示数据。准备好数据后,将显示所有数据。另外,我现在只使用一个计时器,而不是两个。

:事件处理方法可能在一个线程上运行,同时另一个线程调用Stop方法或将Enabled属性设置为false。这可能会导致计时器停止后引发已用事件。Stop方法的示例代码显示了避免这种竞争条件的一种方法。