2
PopulateTable(DataTable filterTable, TableDef contextTableDef)
{
    string friendlyFieldName = "";
    string friendlyTableName = "";
    string friendlyFullName = "";
    try
    {            
     *For loop commented*
    //foreach (FieldDef field in contextTableDef.Fields) {
    //friendlyTableName = contextTableDef.Description;
    //friendlyFieldName = field.FriendlyName;
    //friendlyFullName = friendlyTableName + "." + friendlyFieldName;
    //if (!field.CurrentView.TimeField) {
        //filterTable.Rows.Add(contextTableDef.Name, friendlyFullName, //contextTableDef.Name, field.Name, friendlyFieldName, friendlyTableName);
    //}

        Parallel.ForEach(contextTableDef.Fields, (field) =>
        {
            friendlyTableName = contextTableDef.Description;
            friendlyFieldName = field.FriendlyName;
            friendlyFullName = friendlyTableName + "." + friendlyFieldName;
            if (!field.CurrentView.TimeField) {
                filterTable.Rows.Add(contextTableDef.Name, friendlyFullName, contextTableDef.Name, field.Name, friendlyFieldName, friendlyTableName);
            }

        });

        foreach (TableDef childTable in contextTableDef.Children) {
            if (childTable.IsOneToOne) {
                PopulateFilterTable(filterTable, childTable);
            }
        }

        return filterTable;

    } catch (Exception ex) {
        throw;
    }
}

Used here Parallel.Foreach instead of for loop to increase performance.Here in code i used custom collection TableDef,FiledDef. When i run the code and observed that the loop completed but the application is hanged not able recognized why is it so.

ND's
  • 2,155
  • 6
  • 38
  • 59
  • 10
    You probably shouldn't manipulate the table inside the parallel code, the DataTable is not thread-safe when writing to it. – Lasse V. Karlsen Dec 01 '15 at 11:14
  • 2
    Same as what Lasse wrote, with an additional link: http://stackoverflow.com/questions/21310757/thread-safety-for-datatable – Jens Dec 01 '15 at 11:15
  • @LasseV.Karlsen: If DataTable is not thread safe as per your suggestion what should i use to increase the performance of traditional for each loop which contains millions of records.Here also you seethe function is calling recursively – ND's Dec 01 '15 at 11:22
  • Use `BlockingCollection` and producer/consumer pattern – VMAtm Dec 01 '15 at 11:41
  • 3
    Well... don't put the millions of records in a DataTable? Why do you put them in a DataTable? – Luaan Dec 01 '15 at 11:49
  • What makes you think the loop runs infinitely? It's more likely to **run for a long time**. What kind of analysis did you make? Try to increment a counter (with `Interlocked.Increment`) and output it every 1000 times to see the execution time of your loop. – shay__ Dec 01 '15 at 16:25
  • Actually the application is hang when used Parallel.ForEach – ND's Dec 02 '15 at 04:54
  • That's not enough. **Where** does the application hang? At what line exactly? – shay__ Dec 02 '15 at 07:58
  • @VMAtm: How should implement BlockingCollection and producer/consumer pattern in my function – ND's Dec 02 '15 at 09:42
  • one thread is doing the recursive add, one thread is consuming the blocking collection. After recursive is over, complete the collection. – VMAtm Dec 02 '15 at 10:04
  • @VMAtm: Can u please code for this,didn' have any idea about your suggested approach. – ND's Dec 02 '15 at 10:38
  • You really should google samples for this. This is quite easy and you can do that by yourself. – VMAtm Dec 02 '15 at 11:16
  • @NKD note that this will not solve your issue. You should of course use thread safe collection (such as `BlockingCollection` as VMAtm suggested), **but that will not solve your issue**. – shay__ Dec 03 '15 at 07:37

0 Answers0