向文本框添加计时器
本文关键字:计时器 添加 文本 | 更新日期: 2023-09-27 18:06:07
我创建了2个字典。一本字典,我把信息保存在里面。然后我将根据值对字典中的信息进行排序。然后,我将把信息保存在第二个字典中。我将显示来自第二个Dictionary的信息。我想要的是每3秒出现一条信息。我可以这么做吗?
//intialize to get and set the coodrdinats
int xCoor;
int yCoor;
int prioritySaver;
int timeSaver;
//Intialize the Dictionaries
Dictionary<int, int> pQueuValues = new Dictionary<int, int>();
//Intialize the Lists
List<int> saveRandomTime = new List<int>();
List<int> randomListxCoor = new List<int>();
List<int> randomListyCoor = new List<int>();
private void PQueue(int nodenum)
{
for (int x = 1; x <= nodenum; x++)
{
xCoor = coor.Next(0, 700);
yCoor = coor.Next(0, 730);
if (!randomListxCoor.Contains(xCoor))
{
randomListxCoor.Add(xCoor);
}
if (!randomListyCoor.Contains(xCoor))
{
randomListyCoor.Add(yCoor);
}
prioritySaver = pQueuNumbers.Next(1, nodenum*3);
timeSaver = randomTime.Next(1, 201);
//Add the values t the Dictionaries and the List.
pQueuValues.Add(x, prioritySaver);
}
//Sort the Dictionary
foreach (KeyValuePair<int, Int32> savePQ in pQueuValues.OrderBy(key => key.Value))
{
txtOutput.Text += "'r'n'n" + "The node number :" + savePQ.Key + " Has the Prority: " + savePQ.Value;
}
//I want the timer the show the infroamtion from the sorted dictionary.
}
}
我想做的事情也像这样。但我得到的问题与下面的代码,在打印字典中的所有值。我想让它一次打印一个值。
//Sort the Dictionary
foreach (KeyValuePair<int, Int32> savePQ in pQueuValues.OrderBy(key => key.Value))
{
pQueueOrdered.Add(savePQ.Key, savePQ.Value);
//txtOutput.Text += "'r'n'n" + "The node number :" + savePQ.Key + " Has the Prority: " + savePQ.Value;
}
var timer = new System.Windows.Forms.Timer();
int track = 0;
timer.Tick += (timerObject, timerArgs) =>
{
timer.Interval = 3000;
txtOutput.Text += "'r'n'n" + "The node number :" + pQueueOrdered.ContainsKey(track) + " Has the Prority: " + pQueueOrdered[track];
}
++track;
if (track > nodenum+1)
{
timer.Stop();
timer.Dispose();
this.txtOutput.Text += "'r'r'n" + " Ends x " + track.ToString();
}
};
timer.Start();
}
您的代码有点难以理解。例如,您正在更新不使用的变量。
在任何情况下,你都应该考虑使用微软的响应式框架。(NuGet Rx-Main。)
这里是你需要的代码,因为我可以最好地确定:
private Random rnd = new Random();
private void PQueue(int nodenum)
{
var txtOutput_Text = "";
var queue =
from x in Enumerable.Range(1, nodenum)
let v = new { x, ps = rnd.Next(1, nodenum * 3) }
orderby v.ps
select v;
Observable
.Interval(TimeSpan.FromSeconds(1.0))
.Zip(queue.ToObservable(), (i, v) => v)
.ObserveOn(txtOutput)
.Subscribe(v =>
{
txtOutput.Text +=
"'r'n'n"
+ "The node number :" + v.x
+ " Has the Prority: " + v.ps;
}, () =>
{
txtOutput.Text +=
"'r'r'n"
+ " Ends x " + nodenum.ToString();
});
}
基于您现在拥有的代码和模糊的描述以及您的代码blob不可编译的事实,我认为这接近您需要的。我假设你想点击一个按钮来启动计时器,并将所有内容都包含在按钮的OnClick事件中。
public void Button1_Click(object sender,EventArgs e)
{
var timer = new System.Windows.Forms.Timer();
timer.Interval = 3000; // every 3 seconds
int track = 0; // where are we, aka the index
txtOutput.Text = String.Empty;
var output = new StringBuilder(); // use a stringbuilder
timer.Tick += (timerObject, timerArgs) =>
{
// check if track exists
// I couldn't find the type of pQueueOrdered so
// I hope this works
// otherwise track<=nodeNum might be an alternative
if (pQueueOrdered.ContainsKey(track))
{
output.AppendFormat(String.Format(
"'r'n'nThe node number :{0} Has the Prority: {1}",
pQueueOrdered.ContainsKey(track),
pQueueOrdered[track]));
txtOutput.Text = output.ToString();
// increase track to get the next
// item at the next Tick
track++;
}
// stop if done...
if (track > nodenum+1)
{
timer.Stop();
output.AppendFormat(String.Format(
"'r'r'n Ends x {0}",
track));
txtOutput.Text = output.ToString();
}
}
timer.Start();
}