2009-04-28 8 views
2

장치에 설치된 타사 메시지 큐 소프트웨어를 사용하는 .NET Compact Framework 응용 프로그램을 C#으로 개발 중입니다.응용 프로그램의 평생 동안 닷넷 CF 실행 스레드

저는이 환경에 상당히 익숙하며, 올바른 방향으로 가고 있는지, 또는 개선 할 수있는지를보기 위해 지혜로운 과거의 몇 가지 핵심 개념을 아키텍처에 적용 할 수 있는지 궁금합니다.

내 응용 프로그램이 실행되는 동안 백그라운드에서 실행중인 메시지 큐 라이브러리를 유지해야 알림이 발생하고 내 응용 프로그램에 내 응용 프로그램에 알리고 응용 프로그램에서 생성 한 메시지를 처리 ​​할 수 ​​있습니다. 내 응용 프로그램의 수명 내내 실행해야하므로 응용 프로그램이 시작될 때 자체 스레드에서 실행하는 것이 가장 좋은 방법이라고 생각합니까?

나는이 게시물을 코드로 넘치고 싶지 않았기 때문에 섹션을 중요시하다고 생각했습니다. 더 명확히해야하는지 알려주세요. 응용 프로그램은 다음과 같이 시작할 때

나는

[MTAThread] 
static void Main() 
{ 
    //Run the message processor in its own thread 
Thread messagingThread = new Thread(new ThreadStart(msgProcessorStart)); 
messagingThread.Priority = ThreadPriority.Highest; 
messagingThread.Start(); 

….. 
Application.Run(splashForm); 
} 

private static void msgProcessorStart() 
{ 
MessageProcessor.Start();      
} 

MessageProcessor 상호 작용을 단순화하고 그것의 단일 인스턴스를 유지하는 메시징 라이브러리를 통해 외관이며, 별도의 스레드에서 메시지 처리를 시작합니다. 나는 그것의 일부를 아래에 게시했다. 배달되지 않는 이벤트를 발생시키고 메시지가 수신 될 때이를 알린다.

public static class MessageProcessor 
    { 
     #region Declarations 

//private static reference to the MessageProvider as specified in the configuration files. 
private static readonly IMessageProcessor _messageProcessor = MessageAccess.MessageProvider; 

     #endregion 

     #region Constructor 

     /// <summary> 
     /// Static constructor, connects to the events on the messageProcessor instance which 
     /// relate to messages received, notifications received and exceptions raised. 
     /// </summary> 
     static MessageProcessor() 
     { 
      //Connect up events specifed on the IMessageProcessor interface. 
      _messageProcessor.MessageReceived += messageReceived; 
     } 

     #endregion 

     #region Public Methods 

     /// <summary> 
     /// Sends a request based data message. 
     /// </summary> 
     /// <typeparam name="T">Message which implements RequestBase</typeparam> 
     /// <param name="message">The message to send</param> 
     public static void SendMessage<T>(T message) where T : RequestBase 
     { 
      _messageProcessor.SendMessage(message); 
     } 

     /// <summary> 
     /// Starts the Message Processor. 
     /// </summary> 
     /// <returns>bool, true if started successfully.</returns> 
     public static void Start() 
     { 
      _messageProcessor.Start(); 
     } 

     #endregion 

     #region Private methods 

     //Registered listener of the IMessageProcessor.MessageReceived event 
     private static void messageReceived(object sender, MsgEventArgs<IMessage> message) 
     { 
      ThreadPool.QueueUserWorkItem(new WaitCallback(processMessage),(object)message.Value); 
     } 


     //Invoked via messageReceived. 
     private static void processMessage(object msg) 
     {    
      //process the message 
     } 

     #endregion 
    } 

Start 메서드는 먼저 호출되어 세션을 설정합니다. 그러면 알림을 받고 메시지를 보낼 수있게됩니다.

메시지를 받으면 나는 현재 다른 알림 및 메시지에 계속 응답하기 위해 ThreadPool을 통해 별도의 스레드에서 이벤트 처리를 관리하고 있습니다.

이 방법이 합리적인 방법으로 보이고 내 메시지 대기 라이브러리가 내 응용 프로그램과 별도로 처리 할 수 ​​있습니까?

감사의 말을 들어 주시면 감사하겠습니다.

답변

0

개념적으로 별도의 스레드에 넣는 것이 올바른 선택입니다.

배경 스레드가 작동을 중지 할 수 있습니다 몇 가지 조건이 있습니다

  1. 전체 응용 프로그램을 닫았습니다 (받은 WM_CLOSE 메시지)
  2. 예외가 발생하고
  3. 운영 체제가 백그라운드 스레드 컨텍스트에서 적발되지 않았다

코드에서 조건 번호 2 만 방지 할 수 있습니다.

완벽한 격리를 원할 경우 Windows 서비스를 작성하여 장치에 설치할 수 있습니다. 그러나 .NET CF 서비스가 기본적으로 사용 가능하다고 생각하지 않습니다. 그러나이 장애물을 극복하는 데 사용할 수있는 구현은 out there입니다.

또 다른 방법은 해당 루프 및 숨겨진 기본 창과 별도의 응용 프로그램을 만드는 것입니다.

0

매우 합리적인 것 같습니다. 나는 일반적으로 DI 프레임 워크를 사용하기 때문에 구현 방식이 약간 다르게 구현되었지만 개념은 동일했을 것입니다. 한 가지 추가 할 것은 true 스레드의 IsBackground 속성을 설정하는 것입니다.

관련 문제