新手求助:按时间输出数组
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using ThreadingTimer = System.Threading.Timer;
using TimersTimer = System.Timers.Timer;
namespace Timer_Threading
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ThreadingTimer _ThreadTimer = null;
ArrayList TextValueSeries = new ArrayList();
float order;
string aaaa;
private void Form1_Load(object sender, EventArgs e)
{
}
delegate void UpdateControl(Control Ctrl, string Msg);
private object _objLock = new object();
void _mUpdateControl(Control Ctrl, string Msg)
{
lock (this._objLock)
{
if (Ctrl is ListBox)
((ListBox)Ctrl).Items.Add(Msg);
}
}
private void button1_Click(object sender, EventArgs e)
{
float j = 0;
order = float.Parse(textBox1.Text);
for (int i = 0; i < order ; i++)
{
TextValueSeries.Add(j);
j++;
}
TextValueSeries.TrimToSize();
string currentName = new StackTrace(true).GetFrame(0).GetMethod().Name;
this._ThreadTimer = new ThreadingTimer(new System.Threading.TimerCallback(CallbackMethod), currentName,1000,100);
}
void CallbackMethod(object State)
{
float[] TestValue = new float[TextValueSeries.Count];
TextValueSeries.CopyTo(TestValue);
/*for(int i=0;i<TextValueSeries.Count;i++)
{
string aaaa = string.Format("{0}", TestValue[i]);
}*/
for (int i = 0; i < TextValueSeries.Count; i++)
{
aaaa = string.Format("{0}", i);
}
this.BeginInvoke(new UpdateControl(_mUpdateControl), new object[] { this.listBox1,aaaa});
}
}
}
在编一个和时间有关的计算程序,准备利用Timers.Threading来把数学公式中计算的时间和计算机时间同步来模拟一个控制系统,上面的程序是一个测试
问题:
数组赋值后,按时间输出时每次都输出最后一个数值,感觉是循环的位置(红色)搞错了,但是不知道,该在何处放循环,试了好几次,但是总是不能做到按时间步长来输出数据。求高手指点
[解决办法]
for (int i = 0; i < TextValueSeries.Count; i++)
{
aaaa = string.Format("{0}", i);
}
this.BeginInvoke(new UpdateControl(_mUpdateControl), new object[] { this.listBox1,aaaa});
=>
aaaa = string.Format("{0}", TextValueSeries[i++ % TextValueSeries.Count]);
this.BeginInvoke(new UpdateControl(_mUpdateControl), new object[] { this.listBox1,aaaa});
再在外面定义成员变量i:
private int i = 0;