C# 使用Datagridview C进行多线程处理#
这里我正在做一个检查用户名的过程。我已经创建了一个datagridview并从文本文件中加载了数据。因此datagridview在前两列中包含用户的名字和姓氏。我需要做的是逐行读取这些值,发现first和last没有相同的名称。这些操作在单独的类中执行。所以我需要从该类中获取结果,并交替地将结果显示给gridview。当我只使用一个线程时,一切都很好。但当我使用多个线程时,它只会抛出一个异常,即gridview读取中的异常。这是我的代码:C# 使用Datagridview C进行多线程处理#,c#,multithreading,winforms,datagridview,C#,Multithreading,Winforms,Datagridview,这里我正在做一个检查用户名的过程。我已经创建了一个datagridview并从文本文件中加载了数据。因此datagridview在前两列中包含用户的名字和姓氏。我需要做的是逐行读取这些值,发现first和last没有相同的名称。这些操作在单独的类中执行。所以我需要从该类中获取结果,并交替地将结果显示给gridview。当我只使用一个线程时,一切都很好。但当我使用多个线程时,它只会抛出一个异常,即gridview读取中的异常。这是我的代码: static int i, j=0, l=0,k=0,m
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();
}
}
static int i,j=0,l=0,k=0,m=0;
公共作废检查()
{
对于(i=0;i
请建议我如何在这里使用多线程。。。这只是一个例子。。请用其他方式解释
lock
语句防止不同线程同时运行同一代码。您需要一个引用来用作锁的标识符。创建仅用于锁定的简单对象很常见:
static object sync = new Object();
lock (sync)
{
// do multithreaded stuff here
}
UI
对多线程不友好,因此直接更新DataGridView
是不明智的,因为它在编辑值时会刷新。更改其数据源
,并在所有线程完成工作后调用更新
DataSet dataset = dataGridView1.DataSource;
// do threaded operations on dataset
// update the datagrid when threads finish work
dataGridView1.DataSource = dataset;
dataGridView1.Update();
threads[l] = new Thread(new ThreadStart(()=>check( startPosition, endPosition));
BackgroundThread
的不同方法:
此代码有几个问题
DataGridView
。这一点可以从检查中的第一行代码中看出。你根本无法做到这一点
i
、j
、l
,并假定k
和m
(尽管我没有看到它们在任何地方声明)定义为静态变量。其结果是,每个辅助线程都将使用相同的值工作,并且相互踩在对方的脚趾上。对于i
,这是一个更大的问题,因为它实际上是从工作线程访问的。其他的是从匿名委托访问的,匿名委托被封送到UI线程上。不过,这可能会引起很多混乱,因为匿名委托的执行顺序是不可预测的Invoke
。这本身不是问题。但是,考虑到您正在将所有有用的工作混搭回UI线程,这是毫无意义的。想想看。您执行所有这些工作是为了使用辅助线程和