gpt4 book ai didi

c# - 使用 Datagridview C# 的多线程

转载 作者:太空宇宙 更新时间:2023-11-03 11:00:04 25 4
gpt4 key购买 nike

这里我正在做一个检查用户名的过程。我已经创建了一个 datagridview 并从文本文件加载了数据。所以 datagridview 在前两列中包含用户的名字和姓氏。我需要做的是逐行读取这些值,发现 first 和 last 没有相同的名称。这些操作在单独的类中执行。所以我需要从该类中获取结果并将结果交替显示给 gridview。当我只使用一个线程时一切正常。但是当我使用多个线程时,它只是抛出一个异常,即 gridview reading 中的 outofbound exception。这是我的编码:

static int i, j=0, l=0,k=0,m=0;

public void check()
{
for (i = 0; i < dataGridView1.Rows.Count ; i++)
{
if (InvokeRequired)
{
Invoke(new UpdateDelegate(delegate
{
if (i == 0 && j==0)
{
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Status";
int colIndex = dataGridView1.Columns.Add(col);
dataGridView1.Rows[i].Cells[colIndex].Value = "Process Started";
j = 1;
}
else
{
dataGridView1.Rows[i].Cells[3].Value = "process Started";
}
}));
}

if (InvokeRequired)
{
Invoke(new UpdateDelegate(delegate
{
Process obj_reg = new Process(dataGridView1.Rows[i].Cells[1].Value.ToString(),dataGridView1.Rows[i].Cells[2].Value.ToString());
string res = obj_reg.register();
Thread.Sleep(500);
if (res.Contains("success"))
{
if (i == 0 && k==0)
{
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Result";
int colIndex = dataGridView1.Columns.Add(col);
dataGridView1.Rows[i].Cells[colIndex].Value = "Ya its different";
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;
k = 1;
}
else
{
dataGridView1.Rows[i].Cells[4].Value = "Ya its different";
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;
}
}
else
{
if (i == 0 && m == 0)
{
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Result";
int colIndex = dataGridView1.Columns.Add(col);
dataGridView1.Rows[i].Cells[colIndex].Value = "No its same";
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
m = 1;
}
else
{
dataGridView1.Rows[i].Cells[4].Value = "No its same";
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;

}
}
}));
}
}
}

public void Button1_Click(Object sender, EventArgs e)
{
Thread[] threads = new Thread[3];
for (int l = 0; l < 3; l++)
{
threads[l] = new Thread(new ThreadStart(check));
}
foreach (Thread t in threads)
{
t.Start();
}
}

请建议我如何在这里使用多线程...这只是一个例子..请解释任何其他方式..

最佳答案

这段代码有几个问题。

  1. 您正在从工作线程访问 DataGridViewcheck 中的第一行代码证明了这一点。你根本无法做到这一点。

  2. 您定义了变量 ijl,并且可能还定义了 km(虽然我没有看到它们在任何地方声明)为静态。结果是每个工作线程将使用相同的值并互相踩踏。对于 i 来说这是一个更大的问题,因为它实际上是从工作线程访问的。其他的是从编码到 UI 线程的匿名委托(delegate)访问的。不过,这可能会引起很多困惑,因为匿名委托(delegate)的执行顺序是不可预测的。

  3. 您正在从工作线程调用 Invoke。这本身不是问题。但是,考虑到您正在将所有有用的工作混搭回 UI 线程,这是毫无意义的。想想看。您完成了所有工作以使用工作线程,然后无论如何都将所有内容编码回 UI 线程。您现在拥有的解决方案比根本不使用任何工作线程的情况更糟糕。

  4. 这真的不是问题,但我有更多的提示。为什么要调用 InvokeRequired?您已经知道“调用”是必需的,因为您在开发时就知道这些东西在工作线程上。

我的建议如下。

  1. 根本不要使用线程。在这种情况下是没有意义的。可以在另一个线程上执行的唯一有用的工作是比较名字和姓氏。这很简单,可以在 UI 线程上同样快速地完成。

  2. 如果您真的想使用单独的线程,那么您必须从 UI 线程上的 DataGridView 中读取值并将它们放入单独的数据中结构;一个可以安全地从另一个线程使用的。最好将此数据结构与 DataGridView 一起维护,这样当需要启动长时间运行的操作时,您将不需要读取网格,因为您已经将数据复制到此独立的数据结构。

关于c# - 使用 Datagridview C# 的多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18015371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com