C# 同时更新10个任务中的10个列表框
如果这很简单,请原谅我。我来自嵌入式实时操作系统世界,学习c#只是为了生产测试设备。我想我已经解决了所有明显的问题,只是任务没有按预期进行 我有一个带有10个串行端口和其他附件的测试夹具,还有一个带有10个列表框的非常基本的windows窗体UI,需要同时运行。我已经将代码缩减到演示问题所需的程度(没有串行端口等) 我无法从10个任务中正确更新10个列表框(或文本框)。我可以很容易地一次做一个,甚至5个都可以,但10个都不行 我得到的异常超出了范围,因为它以某种方式通过了10,但我只通过了0-9,所以我现在将任务计数减少到9,直到我理解了基本问题 这是来自PTT类的任务,它将管理和监控10个任务,因此UI仍然响应C# 同时更新10个任务中的10个列表框,c#,task,C#,Task,如果这很简单,请原谅我。我来自嵌入式实时操作系统世界,学习c#只是为了生产测试设备。我想我已经解决了所有明显的问题,只是任务没有按预期进行 我有一个带有10个串行端口和其他附件的测试夹具,还有一个带有10个列表框的非常基本的windows窗体UI,需要同时运行。我已经将代码缩减到演示问题所需的程度(没有串行端口等) 我无法从10个任务中正确更新10个列表框(或文本框)。我可以很容易地一次做一个,甚至5个都可以,但10个都不行 我得到的异常超出了范围,因为它以某种方式通过了10,但我只通过了0-9
public static void ProductionTest()
{
//Somewhere to store the tasks
List<Task> TestSlotTasks = new List<Task>();
//Create the tasks for each of the test slots
for (int i = 0; i != 9; ++i)
{
TestSlotTasks.Add(Task.Factory.StartNew(() => new PTT().TestSlot(i)));
Program.ListBoxAddLine(string.Format("CALL[{0}]: task start/stop test", i), System.Drawing.Color.LawnGreen, i);
}
Task.WaitAll(TestSlotTasks.ToArray());
}
这是我用来更新列表框的invoke方法。列表框位于一个类似这样的通用列表中
//ListBoxs.Add(listBoxSlot1); //Done for each of the 10 list box's to put in a list
//This is the method that will add a line to the main text box and to the log file
public static void ListBoxAddLine(string TextToAdd, System.Drawing.Color textColor, int slot)
{
//Update the list box on the main screen
if (mainForm != null)
{
if (FormPTT.ListBoxs[slot].InvokeRequired)
{
FormPTT.ListBoxs[slot].Invoke((MethodInvoker)delegate ()
{
FormPTT.ListBoxs[slot].ForeColor = textColor;
FormPTT.ListBoxs[slot].TopIndex = FormPTT.ListBoxs[slot].Items.Add(TextToAdd);
});
}
else
{
FormPTT.ListBoxs[slot].ForeColor = textColor;
FormPTT.ListBoxs[slot].TopIndex = FormPTT.ListBoxs[slot].Items.Add(TextToAdd);
}
}
}
对于这个例子,如果我超过5或6个列表框,我得到的输出是非常随机的
使用上面的代码,我得到了这个
因此,在插槽8之前一切正常,它显示调用[7]任务开始/停止,但任务调用中没有任何内容,9和10都正常,但我不会调用它10次,那么10是如何更新的呢?我不及格10分?8的PTT[7]在哪里。。。输出
我想我只是不了解一些关于任务的事情…你能把你正在使用的UI技术添加到标签上吗?Winforms或WPF?用于循环do'int tmp=i;'并将该“tmp”而不是“i”传递给新的PTT().TestSlot()调用。首先是循环变量上的修改闭包。“我假设我只是不了解任务的一些方面…”,任务本质上是在线程池上运行的托管作业。在这种情况下,您需要调用UI线程从后台线程或任务对UI进行更改。请将您正在使用的UI技术添加到标记中,好吗?Winforms或WPF?用于循环do'int tmp=i;'并将该“tmp”而不是“i”传递给新的PTT().TestSlot()调用。首先是循环变量上的修改闭包。“我假设我只是不了解任务的一些方面…”,任务本质上是在线程池上运行的托管作业。在这种情况下,您需要调用UI线程来从后台线程或任务对UI进行更改。
private void TestSlot(int slot)
{
for (var z = 0; z != 10; ++z)
{
Program.ListBoxAddLine(string.Format("PTT[{0}]: task start/stop test", slot), System.Drawing.Color.LawnGreen, slot);
}
}
//ListBoxs.Add(listBoxSlot1); //Done for each of the 10 list box's to put in a list
//This is the method that will add a line to the main text box and to the log file
public static void ListBoxAddLine(string TextToAdd, System.Drawing.Color textColor, int slot)
{
//Update the list box on the main screen
if (mainForm != null)
{
if (FormPTT.ListBoxs[slot].InvokeRequired)
{
FormPTT.ListBoxs[slot].Invoke((MethodInvoker)delegate ()
{
FormPTT.ListBoxs[slot].ForeColor = textColor;
FormPTT.ListBoxs[slot].TopIndex = FormPTT.ListBoxs[slot].Items.Add(TextToAdd);
});
}
else
{
FormPTT.ListBoxs[slot].ForeColor = textColor;
FormPTT.ListBoxs[slot].TopIndex = FormPTT.ListBoxs[slot].Items.Add(TextToAdd);
}
}
}