2010-03-20 6 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 문 주위의 주요 코드를 포장이다 현재 쓰레드가 끝날 때까지.

희망이 도움이됩니다.

관련 문제