简体   繁体   English

使用多线程C#将许多行插入DatagridView?

[英]Insert many row to DatagridView using Multiple threads C#?

I'm doing scrap program from website. 我正在从网站上执行废品程序。 I have many threads to update info to datagridview. 我有很多线程可以将信息更新到datagridview。 I'm using dataset to set binding source to datagridview. 我正在使用数据集将绑定源设置为datagridview。 When i inserted about 100k row. 当我插入约10万行时。 My GUI program show "not responding". 我的GUI程序显示“无响应”。 I don't know how to solve it. 我不知道该怎么解决。 This is my delegate to insert : 这是我的代表要插入:

public void InsertLine(string line)
{
  this.MyDV.BeginInvoke(new MethodInvoker(delegate()
  {                 
    string[] park = Regex.Split(line, @",");
    try
    {
        //Insert new row
        MyDatasset.MyTableRow row = this.MyDataSet.MyTable.NewMyTableRow();
        row.Message = park[0].Trim();
        row.From = park[1].Trim();              
        this.MyDataSet.MyTable.Rows.Add(row);

        //Set color text for new row
        DataGridViewRow myrow = (from DataGridViewRow r in MyDV.Rows
                                 where (long)r.Cells[clId.Name].Value == row.Id
                                 select r).FirstOrDefault();
        if (myrow != null)
        {
            myrow.Cells[clFrom.Name].Style.ForeColor = Color.Blue;
            myrow.Cells[clMessage.Name].Style.ForeColor = Color.Blue;
        }       
    }
    catch { }
    try
    {
        this.MyDV.FirstDisplayedScrollingRowIndex = this.MyDV.Rows[this.MyDV.Rows.Count - 2].Index; //Scroll to lastest row
    }
    catch { }       
}));  }

Can you guys help me? 你们能帮我吗? I'm getting confused many hours with it. 我对它迷惑了许多小时。 Thanks in advance. 提前致谢。

What you are doing is invoking that code on the UI thread. 您正在做的是在UI线程上调用该代码。 It's a loop that iterates potentially many rows. 这是一个循环,可能会迭代许多行。 This is probably taking a long time and blocking the UI thread from doing anything else. 这可能会花费很长时间,并阻止UI线程执行任何其他操作。 You could try breaking up the work into smaller (or fixed-size) chunks and individually invoking them--which would allow the UI thread to process other messages in between. 您可以尝试将工作分解为较小(或固定大小)的块并分别调用它们-这将允许UI线程在其间处理其他消息。

Alternatively, you could do the work in the Application.Idle event; 或者,您可以在Application.Idle事件中完成工作; but, you'd likely still have to break up the work into fixed-sized chunks because you should only take a small, ideally fixed, amount of time in the Idle event. 但是,您可能仍需要将工作分解成固定大小的块,因为在Idle事件中您应该只花很少的时间,最好是固定的时间。

I think the problem that you have here is that you are not locking the code above and it is been accessed by multiple threads all at the same time. 我认为这里的问题是您没有锁定上面的代码,并且所有线程同时访问了所有代码。

You need to use :- 您需要使用:-

Lock (this)

{

this.MyDV.BeginInvoke(new MethodInvoker(delegate()
  {                 
    string[] park = Regex.Split(line, @",");
    try
    {
        //Insert new row
        MyDatasset.MyTableRow row = this.MyDataSet.MyTable.NewMyTableRow();
        row.Message = park[0].Trim();
        row.From = park[1].Trim();              
        this.MyDataSet.MyTable.Rows.Add(row);

        //Set color text for new row
        DataGridViewRow myrow = (from DataGridViewRow r in MyDV.Rows
                                 where (long)r.Cells[clId.Name].Value == row.Id
                                 select r).FirstOrDefault();
        if (myrow != null)
        {
            myrow.Cells[clFrom.Name].Style.ForeColor = Color.Blue;
            myrow.Cells[clMessage.Name].Style.ForeColor = Color.Blue;
        }       
    }
    catch { }
    try
    {
        this.MyDV.FirstDisplayedScrollingRowIndex = this.MyDV.Rows[this.MyDV.Rows.Count - 2].Index; //Scroll to lastest row
    }
    catch { }       
}));  }

}

This should make each thread wait in turn to use your insert delegate. 这应该使每个线程依次等待使用您的插入委托。

Also check out :- http://msdn.microsoft.com/en-us/library/c5kehkcz(v=vs.71).aspx 另外请查看: -http : //msdn.microsoft.com/zh-cn/library/c5kehkcz(v=vs.71).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM