我正在使用控制台应用程序,我在同一个中使用了多线程。我只想知道哪些部分必须放在关键部分内,我的代码是: 。--------------------------------- -----------------------------------------------。
公共类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("===========================================");
}
}
}
请帮我在多线程。 .NET中的我新的话题