Parallel Method Access

(Anyone there)

I am working on a vehicle tracking system: - I have a number of buses                                                                                             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 tires on the list.
Now for each bus in the list I select one bus object and go to the MyMethod (bus bt) method, what I want, I want to transfer b1, b2, b3 to MyMethod (bt bus), and also tell each bus b1-- start its own processing MyMethod (bus bt)
and then for b2 - start its own processing MyMethod (bus bt)
and then for b3 - start its own processing MyMethod (bus bt)
all b1 b2 b3 should start their own processing in parallel (it should be thread safe --- whether it is suitable for using the streaming security of words, I don’t know) ....

--- I tried to use a stream, but the stream does not use the method in parallel ...


more explanation I have only one method and will pass the bus object in a loop to MyMethod (bus bt) one by one ... but I want the stream t1 / t2 ... tn to access this method in parallel ... because that when the thread for b1 starts at the same time for thread b2, which must be executed.

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: - Another synchronization problem ...

  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;
        }

I need to lock my method ... SMSThreadEntryPointFunction (ActiveBusAndItsPathInfoObj)


I'm currently trying

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

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

(Anyone there)

+3
source share
3 answers
ThreadPool.QueueUserWorkItem(state => MyMethod(bus1));
ThreadPool.QueueUserWorkItem(state => MyMethod(bus2));
...
+1
source

The reason your threads are not running in parallel is the line:

threads[indexOfCurrentActiveBusAndItsPathInfoObj].Join();

, . Join() , . , , .

; , , , , . ,

, , . , . , . , . , " ".

+1

BackgroundWorker , . , , . , , muyltiple , , .

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

0
source

Source: https://habr.com/ru/post/1730848/


All Articles