2010-12-31 2 views
0

이 질문은 C#과 관련이 있습니다. 시나리오를 통해 파일 읽기, 데이터 조작 및 파일 덤핑과 같은 작업이 수행 될 것입니다. 내가 상태를 업데이트 각 작업은 UI (FORM-frmTesting)에있는 라벨에 (즉, 파일 읽기 완료, 데이터 조작이 완료)대리인 : 상위 계층에 작업 알리기

버튼 클릭 이벤트는

namespace frmTesting 
{ 
    public partial class Form1 : Form 
    { 
     private void button1_Click_1(object sender, EventArgs e) 
     { 
      class1 l_objClass1 = new class1(); 
      l_objClass1.DoOperation(); 
     } 
    } 

    public class class1 
    { 
     public int DoOperation() 
     { 
      ReadTextFile(); 
      ParsingData(); 
      SaveTextFile(); 
      return 0; 
     } 
     private int ReadTextFile() 
     { 

      //Read the text File 
      return 0; 
     } 
     private int ParsingData() 
     { 
      // Data manipulation 
      return 0; 
     } 
     private int SaveTextFile() 
     { 
      // save the file 
      return 0; 
     } 
    } 
} 

가에 가능하다 델리게이트를 사용합니까?

답변

1
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading; 

namespace WindowsFormsApplication3 
{ 
    public delegate void MyDelagate(); 

    class Class1 
    { 
     public event MyDelagate _myDelegate; 
     private String s1 = String.Empty; 

     public String s 
     { 
      get 
      { 
       return s1; 
      } 

      set 
      { 
       s1 = value; 
       if(_myDelegate != null) 
        _myDelegate(); 
      } 
     } 
     public int DoOperation() 
     { 
      s = "Started"; 
      ReadTextFile(); 
      ParsingData(); 
      SaveTextFile(); 
      s = "Completed"; 
      return 0; 
     } 
     private int ReadTextFile() 
     { 
      s = "Read Text File"; 
      Thread.Sleep(3000);    
      return 0; 
     } 
     private int ParsingData() 
     { 
      s = "Parsing Data"; 
      Thread.Sleep(3000); 
      return 0; 
     } 
     private int SaveTextFile() 
     { 
      s = "Save Text File"; 
      Thread.Sleep(3000); 
      return 0; 
     } 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication3 
{ 
    public partial class Form1 : Form 
    { 
     Class1 x = new Class1(); 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      x._myDelegate += new MyDelagate(UpdateStatus); 
      x.DoOperation(); 
     } 

     void UpdateStatus() 
     { 
      label1.Text = x.s; 
      Validate(); 
     } 
    } 
} 
+0

이것은 '속성'을 사용하는 것이 좋습니다. 대신에'Method'를 사용하십시오. 특정 코드 또는 특정 액션 (여기서는 로깅)의 실행을 위해 'Property'에 의존해서는 안됩니다. 이것은 단순히 나쁜 습관 일뿐입니다. – decyclone

+0

아니요, 이것은 일반적인 관행입니다. 재산 작성자가 얼마나 많은 종류의 코드를 실행할 것인지는 추측 할 수 없습니다. 그것은 자신의 통제가 아닙니다. –

+0

위의 코드를 해석하면 위임자가 호출 될 때 재귀가 발생하며 각 호출되는 하위 메서드는 차례대로 "s"속성을 설정하므로 반복해서 다시 호출 할 수 있습니다 ... – DRapp

0

확실히.

Class1에 대리인 속성을 설정합니다.

클래스 1을 만든 후에 (또는 생성자에서이 작업을 수행 할 수 있음) 함수를 대리자 속성에 할당합니다.

작업이 진행 중이거나 완료되면 대리인이 null이 아닌지 확인한 다음 원하는 이벤트 인수 (상태 수준, 완료 상태, 그런 종류)가있는 위임을 실행합니다.

대리인에게 전달 된 Form1의 함수는 인수 처리를 처리하고 텍스트 필드에 값을 할당합니다.