views:

41

answers:

2

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

A: 

Hi there.

If you mean to lock a section of code, so that only one thread can access your code at a time, then try this:

Declare an object in your class

public class SendBusReachSMS {
    private object _sync;
}

Then in your threaded code have this:

public void SMSThreadEntryPointFunction(ActiveBusAndItsPathInfo objActiveBusAndItsPathInfo)
    {
        try
        {
            lock(_sync)
            {
              // Do Code.....
            }
        // rest of code.

The idea is that you wrap the main code around a lock statement which will block other threads until the current thread has finished.

Hope this helps.

Cheers. Jas.

Jason Evans
A: 

Generally you have to identify the objects that are shared between threads. That is obviously objActiveBusAndItsPathInfo. You have potential problem if two threads get the instance of the same object, and modification of property of this shared object on 1st thread can affect the behaviour of the 2nd thread. But looking at SMSThreadEntryPointFunction I see no such danger, assuming that number is passed to get_TrackingInfoForSendingSMS as value (or I have overlooked something).

dma_k