views:

102

answers:

3

(Any One There)

I am working on vehicle tracking system:- I have n number of buses say b1t1(start at 7 am and stop at 7 pm) bt2 (start at 8 am and stop at 8 pm) and bt3 (start at 9 am and stop at 9 pm) ,where t is start time of a bus

now i have such such busses in a list.
now for each bus in a list i pickup one busobject and pass to method MyMethod(bus bt);what i want is ,I want to pass b1,b2,b3 to MyMethod(bus bt) and and of each bus say b1--start its own proccessing of MyMethod(bus bt)
and then for b2 --start its own proccessing of MyMethod(bus bt)
and then for b3----start its own proccessing of MyMethod(bus bt)
all b1 b2 b3 should start there own processing in parallel (must be thread safe---isn it approprate to use word thread safe i donn know)....

---I tried using thread but thread does not acces the method in parallel...


more explanation i have only one method and will be passing bus object in a loop to MyMethod(bus bt) one by one ... but i want thread t1/t2...tn should access this method in parallel...because when thread for b1 is running simultinuously thread for b2 should run.

enter c public bool SchedulerEntryPointFunction()
    {
        Console.WriteLine("Scheduler is  initiated !\n\n");
        bool bSuccess = false;

        Console.WriteLine("Got ActiveBuses and coresponding Paths!\n\n");
        List<ActiveBusAndItsPathInfo> ActiveBusAndItsPathInfoList = BusinessLayer.GetActiveBusAndItsPathInfoList();
        if (ActiveBusAndItsPathInfoList != null)
        {
            Thread[] threads = new Thread[ActiveBusAndItsPathInfoList.Count];
            while (true)
            {
                foreach (ActiveBusAndItsPathInfo ActiveBusAndItsPathInfoObj in ActiveBusAndItsPathInfoList)
                {
                    //Get curent time
                    //compare for time difference less than equal to 5 mins
                    if (ActiveBusAndItsPathInfoObj.isSMSThreadActive == false)
                    {
                        // Console.WriteLine("SMS Thread about to initiate!\n\n");

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

                        //  if(tsa.TotalMinutes > 0 && tsa.TotalMinutes < 5)
                        {
                            ActiveBusAndItsPathInfoObj.isSMSThreadActive = true;

                            ***ThreadStart starter = delegate { SMSThreadEntryPointFunction(ActiveBusAndItsPathInfoObj); };
                            Thread t = new Thread(starter);
                           **//  t.Start();
                            int indexOfCurrentActiveBusAndItsPathInfoObj = ActiveBusAndItsPathInfoList.IndexOf(ActiveBusAndItsPathInfoObj);
                            threads[indexOfCurrentActiveBusAndItsPathInfoObj] = new Thread(starter);
                            threads[indexOfCurrentActiveBusAndItsPathInfoObj].Start();
                            threads[indexOfCurrentActiveBusAndItsPathInfoObj].Join();***
                        }
                    }
                }**


            }
        }

        return bSuccess;
    }

ode here


New Code:- Still giving synchronization issue...

  foreach (ActiveBusAndItsPathInfo ActiveBusAndItsPathInfoObj in ActiveBusAndItsPathInfoList)
                       {
                        //Get curent time
                        //compare for time difference less than equal to 5 mins
                        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)
                            {
                                ActiveBusAndItsPathInfoObj.isSMSThreadActive = true;

                                ThreadPool.QueueUserWorkItem(state => SMSThreadEntryPointFunction(ActiveBusAndItsPathInfoObj)

                        }
                    }


                }
            }

            return bSuccess;
        }

do i have to lock my method ...SMSThreadEntryPointFunction(ActiveBusAndItsPathInfoObj)


currently i am trying

 ThreadPool.QueueUserWorkItem(new WaitCallback(SMSThreadEntryPointFunction), (object)ActiveBusAndItsPathInfoObj);

but giving an error:-"No overload for SMSThreadEntryPointFunction matches delegate system.thread.WaitCallback"

(Any One There)

A: 

You need to use a BackgroundWorker for your method and each method needs to run in it's own thread. So you'll have to assign each method it's own backgroundworker. You need to make sure that any resources that could be accessed by muyltiple threads at once are locked appropriately so that things are thread-safe.

Thread-safety means that multiple threads can use a resource with incurring data corruption or creating race conditions and/or deadlocks.

Tony
hi,Tnx for ur reply,but i ahve only one method and will be passing bus object in a loop to MyMethod(bus bt) one by one ... but i want thread t1/t2...tn should access this method in parallel...because when thread for b1 is running simultinuously thread for b2 should run...
hrishi
So in your loop you'll have to create your backgroundworker each time, which will spawn a new thread each time. Then all your method calls will run independently, make sure you wire up your Completed events of your backgroundworker, to notify you when work is completed
Tony
+1  A: 
ThreadPool.QueueUserWorkItem(state => MyMethod(bus1));
ThreadPool.QueueUserWorkItem(state => MyMethod(bus2));
...
Darin Dimitrov
I dont want any thread to wait for other thread...they should work independentelly...
hrishi
That's exactly what my example does. It starts execution of MyMethod in parallel, passing it different instance of a bus. Maybe you should read a little more about multithreading in the .NET framework. Here's a good start: http://www.yoda.arachsys.com/csharp/threads/
Darin Dimitrov
Hi,It is giving synchronous issue.
hrishi
hi ..added new code in question plz find the new code...do i have to lock my method ...SMSThreadEntryPointFunction(ActiveBusAndItsPathInfoObj)
hrishi
A: 

The reason that you find your threads not executing in parallel is the line:

threads[indexOfCurrentActiveBusAndItsPathInfoObj].Join();

This causes the main thread to wait for the current bus thread to finish before the main thread continues. You may want to move the Join() call outside the loop that starts your threads or eliminate it all together. Start by commenting it out so you can see the effect it has.

Getting multithreaded code to work correctly can be challenging; and probably impossible if you don't have a good understanding of what's going on. I second Darin's suggestion that you read through the tutorial at http://yoda.arachsys.com/csharp/threads

Finally, it looks like what you're trying to do is run a simulation. A much simpler approach to this is to set up a priority queue of events ordered by simulation time. The main loop then simply pulls the first event off the queue, updates the simulated time to the event's time, and executes the event. Event handlers can schedule future events by pushing them on to the queue. You can find more about this idea by searching for information on "discrete event simulation".

Dave