简体   繁体   中英

DataGridView and Multi-Threading

I'm trying to convert my foreach method to multi-threading.

I have a datagridview and the method gets the value from cell[0] (which contains a url) and sends it to another method which works with 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.");
}

The above code is working without any problem but sequentially.

Then with this i've tried to convert this code to that to be multithreaded :

Button :

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);
    }
}

and the multi-threaded method is here :

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();    
}

It gets the thread count to run from a numericUpDown control and if I choose the value 10 from the numericUpDown it puts "test" text in the first 10 cells of datagridview and then stops, if i choose 4(or 2,5,7), then it puts the "test" text in the first 4(2,5,7) cells and stops.

It doesn't continue to the next rows after the thread is finished. So I'm trying to fire 5 threads (I always choose it from numericUpDown) and when a thread finish its work, it must go to next row.. How can i solve this problem ?

Also these variables have been declared:

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

from output screen i'm getting

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

Thanks.

The index counter was not incremented in the thread method.

There is still an overlapping problem in the program that I will let you fix. Assuming there are 50 urls, the problem is that thread 1 will update cells 0 to 49, thread 2 will update cells 1 to 49, and so on.

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
        {
        }                           
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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