DataGridView和多线程

本文关键字:多线程 DataGridView | 更新日期: 2023-09-27 18:14:51

我正在尝试将我的foreach方法转换为多线程。

我有一个datagridview,该方法从cell[0](其中包含一个url)获得值,并将其发送到与httpwebrequest一起工作的另一个方法。

public void UrlCheck()
{
    foreach (DataGridViewRow row in dataUrlList.Rows)
    {
        string url= row.Cells[0].Value.ToString();
        try
        {
            string get = getHtml(url);
            //work with get string removed
            if()
            {
                row.Cells[1].Value = "page info here";
            }
            else
            {
                row.Cells[1].Value = "error info here";
            }
        }
        catch
        {
        }                       
    }
    MessageBox.Show("Done.");
}

上面的代码运行正常,但是是顺序的。

然后,我试图将此代码转换为多线程:

按钮:

private void button9_Click(object sender, EventArgs e)
{
    int threadcount = Convert.ToInt32(numThreadSearch.Value);
    ThreadForSearch = new Thread[threadcount];
    checkingForSearch = dataUrlList.Rows.Count;
    isRunningForSearch = true;
    beenCheckedForSearch = 1;
    for (int i = 0; i <= threadcount - 1; i++)
    {
        ThreadForSearch[i] = new Thread(new ParameterizedThreadStart(MultiThreadMet));
        ThreadForSearch[i].Start(i);
    }
}

和多线程方法在这里:

public void MultiThreadMet (object IndexForSearch)
{                
    int index = (int)IndexForSearch;
    DataGridViewRow row = dataUrlList.Rows[index];
    while (isRunningForSearch)
    {
        try
        {
            if (beenCheckedForSearch >= checkingForSearch)
            {
                isRunningForSearch = false;
            }
            if (index >= dataUrlList.Rows.Count)
            {
                ThreadForSearch[index].Abort();
            }                     
            //For just test i'm trying to add "test" in every cell[1] in datagridview
            dataUrlList.Invoke(new Action(() => row.Cells[1].Value = "test"));
            beenCheckedForSearch++;  
        }
        catch
        {
        }                           
    }
    ThreadForSearch[1].Abort();    
}

它得到线程计数从numericUpDown控制运行,如果我从numericUpDown选择值10,它把"测试"文本在datagridview的前10个单元格,然后停止,如果我选择4(或2,5,7),那么它把"测试"文本在前4(2,5,7)单元格和停止。

在线程结束后,它不会继续到下一行。所以我试图触发5个线程(我总是选择它从numericUpDown),当一个线程完成它的工作,它必须去下一行。我怎样才能解决这个问题呢?

这些变量也被声明了:

Thread[] ThreadForSearch;
int beenCheckedForSearch;
int checkingForSearch;
private bool isRunningForSearch;

从输出屏幕我得到

Exception thrown: 'System.Threading.ThreadAbortException' in mscorlib.dll

谢谢。

DataGridView和多线程

在线程方法中索引计数器未加1。

程序中仍然有一个重叠的问题,我会让你修复。假设有50个url,问题是线程1将更新单元格0到49,线程2将更新单元格1到49,以此类推。

public void MultiThreadMet (object IndexForSearch)
{                
    int index = (int)IndexForSearch;
    while (isRunningForSearch)
    {
        try
        {
            if (index >= dataUrlList.Rows.Count)
            {
                return;
            }
            DataGridViewRow row = dataUrlList.Rows[index];
            index++;
            //For just test i'm trying to add "test" in every cell[1] in datagridview
            dataUrlList.Invoke(new Action(() => row.Cells[1].Value = "test"));
        }
        catch
        {
        }                           
    }
}