Aniket Narvankar

Aniket Narvankar

  • 559
  • 2.1k
  • 608k

Multithreading in C#

Nov 21 2021 6:52 AM
public void UpdateLeads()
{
    int resultHome = 0;
    int resultAuto = 0;
    int resultCustomerSource = 0;
    int resultShortName = 0;
    int resultProducerLastName = 0;
    int resultSource = 0;
    int resultSourceOnAgencyId = 0;โ€“โ€“
    int resultSourceonAgencyIdRefNbr = 0;
    int resultCustomer = 0;

    Dictionary<string, DataRow> dictHomeAgenaNumber = new Dictionary<string, DataRow>();
    DataTable dtHomeAgenaNumber = prepareDataDE.GetLeadIds();

    foreach (DataRow dr in dtHomeAgenaNumber.Rows)
    {
        string LeadId = dr["LeadId"].ToString();
        dictHomeAgenaNumber.Add(LeadId, dr);
    }
    
    DataTable dt = prepareDataDE.GetLeadsForProcessing();

    if (dt != null && dt.Rows.Count > 0)
    {
        //Parallel.ForEach(dt.AsEnumerable(),dr =>
        foreach (DataRow dr in dt.Rows)
        {
            string LeadId = dr["LeadId"].ToString();
            if (dictHomeAgenaNumber.TryGetValue(LeadId, out var drHomeAgenaNumber))
            {
                int HomeAgenaNumber = Convert.ToInt32(drHomeAgenaNumber["HomeAgenaNumber"].ToString());
                if (HomeAgenaNumber > 0)
                {
                    prepareDataDE.UpdateMessageForLeads(LeadId, Constant.agenaNumber, 2, -1);
                    continue;
                    //return;
                }
                else
                {
                    resultHome = UpdateHomeLeads(LeadId);
                    if (resultHome > 0)
                    {
                        prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgHomeLeads, 3, -1);
                        UpdateCustomerProspectInfo(LeadId);
                        UpdateOccp(LeadId);
                        SetSelfEmployed(LeadId);
                        UpdatePhonesAndEDD(LeadId);
                        UpdateFormattedPhone(LeadId);
                        SplitPhones(LeadId);
                        resultCustomerSource = UpdateCustomerSource(LeadId);
                    }
                    else if (resultHome == 0)
                    {
                        resultAuto = prepareDataDE.UpdateAutoLeads(LeadId);
                        if (resultAuto > 0)
                        {
                            prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgAutoLeads, 4, -1);
                            UpdateCustomerProspectInfo(LeadId);
                            UpdateOccp(LeadId);
                            SetSelfEmployed(LeadId);
                            UpdatePhonesAndEDD(LeadId);
                            UpdateFormattedPhone(LeadId);
                            SplitPhones(LeadId);
                            resultCustomerSource = UpdateCustomerSource(LeadId);
                        }
                        else if (resultAuto == 0)
                        {
                            prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgAutoHome, 2, -1);
                            //return;
                            continue;
                        }
                        else
                        {
                            //return;
                            continue;
                        }
                    }
                    else
                    {
                        //return;
                        continue;
                    }
                    if (resultCustomerSource > 0)
                    {
                        prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgCustomerSource, 5, -1);
                        resultShortName = UpdateShortName(LeadId);
                        if (resultShortName > 0)
                        {
                            prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgCustShortName, 6, -1);
                            resultProducerLastName = UpdateProducerLastName(LeadId);
                            if (resultProducerLastName >= 0)
                            {
                                if (resultProducerLastName > 0)
                                {
                                    prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgProducerLastName, 7, -1);
                                }
                                UpdateATESSource(LeadId);
                                UpdateATESSourceAuto(LeadId);
                                resultSource = UpdateSource(LeadId);
                                if (resultSource > 0)
                                {
                                    prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgSource, 8, -1);
                                    resultSourceOnAgencyId = UpdateSourceOnAgencyId(LeadId);
                                    if (resultSourceOnAgencyId > 0)
                                    {
                                        prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgSourceOnAgencyId, 9, -1);
                                        resultSourceonAgencyIdRefNbr = UpdateSourceonAgencyIdRefNbr(LeadId);
                                        if (resultSourceonAgencyIdRefNbr > 0)
                                        {
                                            prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgSourceOnReferenceNbr, 10, -1);
                                            resultCustomer = MatchExistingCustomers(LeadId);
                                            if (resultCustomer > 0)
                                            {
                                                prepareDataDE.UpdateMessageForLeads(LeadId, Constant.matchCustomers, 13, -1);
                                                ProfileAnswerResponse response = UpdateProfileAnswer(LeadId);
                                                if (response != null && response.status == "Success")
                                                {
                                                    prepareDataDE.UpdateMessageForLeads(LeadId, Constant.profileAnswer, 14, -1);
                                                    DependentResponse depResponse = AddDependent(LeadId);
                                                    if (depResponse != null && depResponse.Status == "Success")
                                                    {
                                                        prepareDataDE.UpdateMessageForLeads(LeadId, depResponse.depId.ToString() + "" + Constant.dependentInsert, 15, -1);
                                                        int resultAMSExported = UpdateAMSExported(LeadId);
                                                        if (resultAMSExported > 0)
                                                        {
                                                            prepareDataDE.UpdateMessageForLeads(LeadId, Constant.amsexported, 16, 1);
                                                            continue;
                                                        }
                                                        else
                                                        {
                                                            prepareDataDE.UpdateMessageForLeads(LeadId, Constant.amsexportedFailed, 16, -1);
                                                            //return;
                                                            continue;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        prepareDataDE.UpdateMessageForLeads(LeadId, Constant.dependentNotInsert, 15, -1);
                                                        //return;
                                                        continue;
                                                    }
                                                }
                                                else
                                                {
                                                    prepareDataDE.UpdateMessageForLeads(LeadId, Constant.profileAnswerNotUpdated, 14, -1);
                                                    //return;
                                                    continue;
                                                }
                                            }
                                            else
                                            {
                                                if (resultCustomer < 0)
                                                {
                                                    prepareDataDE.UpdateMessageForLeads(LeadId, Constant.matchCustomersNotUpdated, 13, -1);
                                                }
                                                //return;
                                                continue;
                                            }
                                        }
                                        else if (resultSourceonAgencyIdRefNbr == 0)
                                        {
                                            prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgSourceOnReferenceNbrNotUpdated, 10, -1);
                                        }
                                        else
                                        {
                                            //return;
                                            continue;
                                        }
                                    }
                                    else if (resultSourceOnAgencyId == 0)
                                    {
                                        prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgSourceOnAgencyIdNotUpdated, 9, -1);
                                    }
                                    else
                                    {
                                        //return;
                                        continue;
                                    }
                                }
                                else if (resultSource == 0)
                                {
                                    prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgSourceNot, 8, -1);
                                }
                                else
                                {
                                    //return;
                                    continue;
                                }
                            }
                            else
                            {
                                //return;
                                continue;
                            }
                        }
                        else if (resultShortName == 0)
                        {
                            prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgCustShortNameNotUpdated, 6, -1);
                        }
                        else
                        {
                            //return;
                            continue;
                        }
                    }
                    else if (resultCustomerSource == 0)
                    {
                        prepareDataDE.UpdateMessageForLeads(LeadId, Constant.msgCustSourceNotUpdated, 5, -1);
                    }
                    else
                    {
                        //return;
                        continue;
                    }
                }
            }
            else
            {
                prepareDataDE.UpdateMessageForLeads(LeadId, Constant.leadIdNotFound, 2, -1);
            }
        }
        //});
    }
    else
    {
        Console.WriteLine("No leads remaining to Process...");
    }
}

How to rewrite this block of code using multithreading. the reason is first I am getting leads from table using GetLeads and second i am taking Leads for Processing using GetLeadsForProcessing. Then I am using foreach loop on the datatable in which I am getting leads for processing and processing the leads. There are db calls in this loop. Here if there are suppose 5000 records process becomes slow,used parallel.foreach for multithreading but it gives deadlock error,kindly let me know how to use multithreading on this code to run code faster.


Answers (4)