2014-01-28 2 views
-1

별도의 프로젝트 인 dll/assembly와 기본 응용 프로그램이 있습니다. 이것은 C# VS2008입니다.기본 응용 프로그램이 이벤트에 응답하지 않습니까?

일부 특정 문자열이 .txt 파일로 작성되었는지 정기적으로 확인하는 DLL/어셈블리. 트리거 될 때마다 (I 추측 DLL에서) 이벤트를 모니터링하기 위해 노력하고 기본적으로

public delegate void IjmFinishedEventHandler(object sender, EventArgs e); 

public class FijiLauncherControl 
{ 
    System.Timers.Timer _logFileCheckTimer;  // log file status check timer; reading every 1/4 second 

    public event IjmFinishedEventHandler IjmFinished; 

    // Invoke the Changed event; called whenever list changes 
    protected virtual void OnIjmFinished(EventArgs e) 
    { 
     if (IjmFinished != null) 
      IjmFinished(this, e); 

    } 

void _logFileCheckTimer_Elapsed(object sender, EventArgs e) // tick check task finish 
    { 
     if (_processOn && IsLogOn) 
     { 
      try 
      { 
       _processFinished = CheckStatuts(); 

       if (_processFinished) 
       {   
        OnIjmFinished(EventArgs.Empty); 
       } 
      } 
      catch (Exception ex) 
      { 

      } 
     } 
    } 

    public bool CheckStatuts() // check if string is written in file 
     { 
      bool ret; 
      var lastLine = File.ReadAllLines(_logFile).Last(); 

      ret = String.Compare(lastLine, _doneStr) == 0 ? true : false; 

      return ret; 
     } 

    public FijiLauncherControl() //start the timer 
    { 
     _logFileCheckTimer = new System.Timers.Timer(250); // read log 4 times per sec 
     _logFileCheckTimer.Enabled = true; 
     _logFileCheckTimer.Elapsed += new  System.Timers.ElapsedEventHandler(_logFileCheckTimer_Elapsed); 
     } 
    } 

그리고 내 주요 응용 프로그램에서

, 나는 참고로이 DLL을 추가하고 :이 문자열을 발견 한 후에는 이벤트를 발생합니다 특정 문자열이 .txt 파일에 기록됩니다에 의해 그러나

public partial class Window1 : Window 
    { 
      private FijiLauncherControl _fl; 


     void Window1_Unloaded(object sender, RoutedEventArgs e) 
     { 
      _fl.IjmFinished -= new IjmFinishedEventHandler(_fl_IjmFinished); 

     } 

     void Window1_Loaded(object sender, RoutedEventArgs e) 
     { 

      _fl.IjmFinished += new IjmFinishedEventHandler(_fl_IjmFinished); 
     } 

     void _fl_IjmFinished(object sender, EventArgs e) 
     { 
      _vm.IjmFinished = true; 
      System.Media.SystemSounds.Beep.Play();    
     }  

    } 

void _fl_IjmFinished(object sender, EventArgs e) 
     { 
      _vm.IjmFinished = true; 
      System.Media.SystemSounds.Beep.Play();    
     } 

에서 그것은 결코 특정 문자열이 .txt 인 작성 얻을 않은 경우에도 응답하지 않습니다. 마치 행사가 결코 집회에서 해고 당하지 않는 것처럼 보인다. 하지만 어셈블리 내에 중단 점을 설정하면

이상한 점이 있습니다. 중단 점을 if (_processFinished)으로 설정해야합니다. 그러면 OnIjmFinished(EventArgs.Empty)이됩니다. 하지만 그것은 중단 점을 두드리지 않습니다 OnIjmFinished(EventArgs.Empty);에 중단 점을 넣었습니다.

그래서 전체적인 문제는 어셈블리에서 이벤트를 발생시킬 수 있지만 주 응용 프로그램은 이벤트를 수신하지 못하는 것입니다. 나는 이것을 엉망으로 만들기 위해 내가 어디에서 잘못했는지 궁금하다. 누구든지 제안 할 수 있습니까? 나는 모든 의견에 매우 감사한다.

+0

_OnIjmFinished_ 메서드 안에 중단 점을 설정하여 이벤트가 null이 아니고 실제로 실행되는지 확인 했습니까? –

+0

이상한 점이있어서 if (_processFinished)에 중단 점을 설정해야합니다. 그러면 OnIjmFinished (EventArgs.Empty); 하지만 그것은 중단 점을 OnIjmFinished (EventArgs.Empty)에 두었습니다. 그것은 중단 점에 부딪치지 않습니다. –

답변

1

Window1_Loaded이 실제로 연결되어 있는지 확인하십시오. Windows 양식 디자이너를 통해 등록한 경우 코드 숨김이 다시 생성 될 때 때로는 손실 될 수 있습니다.

Window1_Loaded에 대한 모든 참조를 찾았 으면 참조가 없습니다. 문제가 발생하면 디자이너로 가서 WindowLoaded 이벤트에 이벤트 처리기를 다시 연결하십시오.

또한

, 대신 자신의 선언의 SystemEventHandler 대리자를 사용할 수 있습니다

public event EventHandler IjmFinished; 

그리고 당신의 인상 이벤트 방법을 스레드로부터 안전하지 않습니다, 대신이 패턴을 사용한다 :

protected virtual void OnIjmFinished(EventArgs e) 
{ 
    var finished = IjmFinished; 

    if (finished != null) { finished(this, e); } 
} 

현재 진행중인 방식으로 다른 스레드의 가입자가 if 사이에서 등록을 취소하고 이벤트를 발생시켜 디버깅하기가 어려워 질 수 있습니다. NullReferenceException

관련 문제