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
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 atSMSThreadEntryPointFunctionI see no such danger, assuming that number is passed toget_TrackingInfoForSendingSMSas value (or I have overlooked something).