2010-03-20 22 views
0

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

回答

0

通常,您必须确定线程之间共享的对象。那显然是objActiveBusAndItsPathInfo。如果两个线程获取同一对象的实例,并且在第一个线程上修改此共享对象的属性可能会影响第二个线程的行为,则可能会出现问题。但看看SMSThreadEntryPointFunction我看不出这样的危险,假设数字被传递给get_TrackingInfoForSendingSMS作为价值(或者我忽略了某些东西)。

0

如果你的意思是锁定的代码段,所以只有一个线程可以同时访问你的代码,那么试试这个:

在类声明的对象

public class SendBusReachSMS { 
    private object _sync; 
} 

然后在你的线程代码有这样的:

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

的想法是,你缠绕在lock声明,这将阻止其他线程取消主代码直到当前线程完成。

希望这会有所帮助。