2014-10-16 4 views
1

두 개의 클래스 컨트롤러와 CountDownTimer가 있습니다. 컨트롤러가있는 프로젝트는 CountDownTimer 클래스를 사용하여 프로젝트를 참조합니다. CountDownTimer 클래스의 메서드 (TickUpdate)를 사용하면 타이머가 1 초씩 카운트 될 때마다 컨트롤러 클래스의 메서드를 호출 할 수 있습니다. 하지만 순환 종속성 때문에 CountDownTimer 프로젝트에서 컨트롤러 프로젝트를 참조 할 수 없습니다.C# 순환 종속성으로 인해 참조되지 않은 프로젝트에서 메서드 호출

내 질문에, 카운트 다운 클래스에서 TickUpdate 메서드를 호출하는 방법은 무엇입니까?

using SailTimerClassLibrary; 

namespace SailTimerUIProject 
{ 
    public class Controller : ApplicationContext 
    { 
    //Store a reference to the UI 
    internal frmMain MainUI { get; set; } 

    private int seconds = 30; 
    CountDownTimer timer; 

    public Controller() 
    { 

     MainUI = new frmMain(this); 

     //We can do any necessary checks or changes to the MainUI here before it becomes visible 
     MainUI.Show(); 
     timer = new CountDownTimer(seconds); 
     TickUpdate(("" + seconds/60).PadLeft(2, '0') + "m:" + ("" + seconds % 60).PadLeft(2, '0') + "s"); 
    } 

    internal void TickUpdate(string mmss) 
    { 
     MainUI.lblTimer.Text = mmss; 
    } 

    internal void StartTimer() 
    { 
     timer.StartTimer(); 
    } 
} 
} 



namespace SailTimerClassLibrary 
{ 
public class CountDownTimer : ICountDownTimer 
{ 
    private int seconds; // Time in seconds 
    private int reSetValue; // Time in seconds 
    public System.Windows.Forms.Timer timer1; 

    public CountDownTimer(int seconds) 
    { 
     this.seconds = seconds; 
     reSetValue = seconds; 
     timer1 = new System.Windows.Forms.Timer(); 
     timer1.Tick += new EventHandler(timer1_Tick); // Add Handler(timer1_Tick) 
     timer1.Interval = 1000; // 1 second 
     //TickUpdate(("" + seconds/60).PadLeft(2, '0') + "m:" + ("" + seconds % 60).PadLeft(2, '0') + "s"); 
    } 

    public void timer1_Tick(object sender, EventArgs e) 
    { 
     seconds--; // Decrement seconds 
     if (seconds == 0) // Stop Timer at 0 
     { 
      timer1.Stop(); // Stop timer 
     } 
     else 
     { 
      //TickUpdate(convertSecondToMMSS()); 

      if (seconds % 60 == 0 || seconds >= 1 && seconds <= 10) 
      { 
       //TickUpdate(seconds); 
      } 
     } 
    } 

    public void StartTimer() 
    { 
     timer1.Start(); // Start Timer 
    } 

    public string convertSecondToMMSS() 
    { 
     TimeSpan t = TimeSpan.FromSeconds(seconds); 
     string str = string.Format("{0:D2}m:{1:D2}s", //{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms 
      t.Minutes, 
      t.Seconds); 

     return str; 
    } 

    public void StopTimer() 
    { 
     timer1.Stop(); 
    } 

    public void ResetTimer() 
    { 
     timer1.Stop(); 
     seconds = reSetValue; 
     //parent.TickUpdate(convertSecondToMMSS()); 
    } 

    public void SetTimer(int seconds) 
    { 
     timer1.Stop(); 
     this.seconds = seconds; 
     reSetValue = seconds; 
     //parent.TickUpdate(convertSecondToMMSS()); 
    } 
} 
} 
+3

[이벤트] (http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx)에 대해 알아볼 시간. 타이머가 당신에 대해 틱 소비자를 명확히 알 필요가없는 것에 주목하십시오. Timer 's Tick 이벤트에 가입하는 것과 같은 방식으로 CountdownTimer에서 사용자 정의 이벤트를 생성하고 누가 청취하고 있는지 신경 쓰지 않고 외부 세계로 이벤트를 내보낼 수 있습니다. – spender

답변

2

여기 몇 가지 디자인 문제가 당신의 능력을 저해합니다.

CountDownTimer은이 두 프로젝트가 참조하는 보조 프로젝트 또는 클래스 라이브러리에 있어야합니다. 이렇게하면 전체 순환 종속성 문제가 방지됩니다. 컨트롤러는 어떨까요?!
음 ....

CountDownTimer 컨트롤러에 관해서는 알지 못하도록해야합니다. 컨트롤러가 핸들러를 추가 할 수있는 일종의 이벤트를 노출해야하며 컨트롤러는 자체를으로 업데이트 할 수 있습니다.