how to predict which section have to put in critical section in threading

Posted by Lalit Dhake on Stack Overflow See other posts from Stack Overflow or by Lalit Dhake
Published on 2010-03-20T12:06:02Z Indexed on 2010/03/20 12:11 UTC
Read the original article Hit count: 404

Filed under:

Hi , I am using the console application i used multi threading in the same. I just want to know which section have to put inside critical section my code is : .------------------------------------------------------------------------------.
public class SendBusReachSMS {

    public void SchedularEntryPoint()
    {
        try
        {
            List<ActiveBusAndItsPathInfo> ActiveBusAndItsPathInfoList = BusinessLayer.GetActiveBusAndItsPathInfoList();
            if (ActiveBusAndItsPathInfoList != null)
            {
                //SMSThreadEntryPoint smsentrypoint = new SMSThreadEntryPoint();
                while (true)
                {
                    foreach (ActiveBusAndItsPathInfo ActiveBusAndItsPathInfoObj in ActiveBusAndItsPathInfoList)
                    {
                        if (ActiveBusAndItsPathInfoObj.isSMSThreadActive == false)
                        {

                            DateTime CurrentTime = System.DateTime.Now;
                            DateTime Bustime = Convert.ToDateTime(ActiveBusAndItsPathInfoObj.busObj.Timing);
                            TimeSpan tsa = Bustime - CurrentTime;

                            if (tsa.TotalMinutes > 0 && tsa.TotalMinutes < 5)
                            {
                                ThreadStart starter = delegate { SMSThreadEntryPointFunction(ActiveBusAndItsPathInfoObj); };
                                Thread t = new Thread(starter);
                                t.Start();
                                 t.Join();
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("===========================================");
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.InnerException);
            Console.WriteLine("===========================================");
        }
    }


    public void SMSThreadEntryPointFunction(ActiveBusAndItsPathInfo objActiveBusAndItsPathInfo)
    {
        try
        {
            //mutThrd.WaitOne();
            String consoleString = "Thread for " + objActiveBusAndItsPathInfo.busObj.Number + "\t" + " on path " + "\t" + objActiveBusAndItsPathInfo.pathObj.PathId;
            Console.WriteLine(consoleString);
            TrackingInfo trackingObj = new TrackingInfo();
            string strTempBusTime = objActiveBusAndItsPathInfo.busObj.Timing;
            while (true)
            {

                trackingObj = BusinessLayer.get_TrackingInfoForSendingSMS(objActiveBusAndItsPathInfo.busObj.Number);

                if (trackingObj.latitude != 0.0 && trackingObj.longitude != 0.0)
                {
                    //calculate distance
                    double distanceOfCurrentToDestination = 4.45;
                    TimeSpan CurrentTime = System.DateTime.Now.TimeOfDay;
                    TimeSpan timeLimit = objActiveBusAndItsPathInfo.sessionInTime - CurrentTime;
                    if ((distanceOfCurrentToDestination <= 5) && (timeLimit.TotalMinutes <= 5))
                    {
                        Console.WriteLine("Message sent to bus number's parents: " + objActiveBusAndItsPathInfo.busObj.Number);
                        break;
                    }
                }
            }
           // mutThrd.ReleaseMutex();
        }
        catch (Exception ex)
        {
            //throw;
            Console.WriteLine("===========================================");
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.InnerException);
            Console.WriteLine("===========================================");
        }

    }

}

Please help me in multithreading. new topic for me in .net

© Stack Overflow or respective owner

Related posts about multithreading