AboutDelegate.zip



시나리오.


학교에 있는 학생을 도서관으로 이동하여 비동기 적으로 공부를 수행하고 끝난 후 다시 학교로 복귀하도록 구성하였다.


Main class

  1 namespace AboutDelegate

  2 {
  3     class Program
  4     {
  5         static void Main(string[] args)
  6         {
  7             School sc = new School();
  8             sc.GoLibrary();
  9             Console.WriteLine("호호호~~");
 10             Thread.Sleep(2500);
 11             Console.WriteLine("호호호~~");
 12             Thread.Sleep(2500);
 13             Console.WriteLine("호호호~~");
 14             Thread.Sleep(2500);
 15             Console.WriteLine("호호호~~");
 16             Thread.Sleep(2500);
 17             Console.ReadLine();
 18         }
 19     }
 20 }



Stu class

  1 using System;

  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading;
  6 
  7 namespace AboutDelegate
  8 {
  9     public class Stu
 10     {
 11         int num;
 12         string name;
 13         public Stu(int _num, string _name)
 14         {
 15             num = _num;
 16             name = _name;
 17         }
 18         public string Name
 19         {
 20             get
 21             {
 22                 return name;
 23             }
 24         }
 25         public int Num
 26         {
 27             get
 28             {
 29                 return num;
 30             }
 31         }
 32 
 33         public void Study(int cnt)
 34         {
 35             for (int i = 0; i < cnt; i++)
 36             {
 37                 Console.WriteLine("{0} 학생 {1} 시간째 공부중", Name, i+1);
 38                 Thread.Sleep(2500);
 39             }
 40         }
 41     }
 42 }
 43 


School class


  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Runtime.Remoting.Messaging;
  6 
  7 namespace AboutDelegate
  8 {    
  9     class School
 10     {
 11        
 12         Library lb=null;
 13         List<Stu> list = new List<Stu>();
 14         public School()
 15         {
 16             lb = new Library();
 17             Stu s = new Stu(1, "조스타");
 18             lb.AddStu(s);
 19             Stu s1 = new Stu(2, "좆스타");
 20             lb.AddStu(s1);
 21             Stu s2 = new Stu(3, "좆밥타");
 22             lb.AddStu(s2);
 23             Stu s3 = new Stu(4, "조스타킹");
 24             lb.AddStu(s3);
 25             Stu s4 = new Stu(5, "조조전");
 26             lb.AddStu(s4); 
 27             lb.SchoolCallEventHandler += new CallBackTest(EndStudy);
             //Library 에있는 SchoolCallEventHandler에 EndSTudy 함수를 등록한다(함수 포인터와 비슷)
 28         }
 29 
 30         public void GoLibrary()
 31         {
 32             lb.StartStudy();
 33         }
 34         public void EndStudy(Stu stu)
 35         {
 36             Console.WriteLine("{0} 학생 공부 완료", stu.Name);
 37             list.Add(stu);
 38             Console.WriteLine("{0} 학생 학교 복귀", stu.Name);
               // 학생 복귀
 39         }
 40     }
 41 }
 42 
 


Libray class

  1 using System;

  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Runtime.Remoting.Messaging;
  6 
  7 namespace AboutDelegate
  8 {
  9     public delegate void CallBackTest(Stu stu); 
 10     class Library
 11     {
 12         delegate void OnStudy(Stu s,int cnt);
 13         public event CallBackTest SchoolCallEventHandler; CallBackTest 이벤트 선언하여 School에서 사용
 14 
 15         List<Stu> list = new List<Stu>();
 16 
 17         public Library()
 18         {
 19         }
 20         public void AddStu(Stu s)
 21         {
 22             list.Add(s);
 23         }
 24         public void StartStudy()
 25         {            
 26             Random rand = new Random();
 27             OnStudy os;
 28             if (SchoolCallEventHandler != null)
 29             {
 30                 foreach (Stu stu in list)
 31                 {
 32                     os = new OnStudy(StudyOn); //학생마다 비동기적으로 동작해야 하므로 매번생성
 33                  os.BeginInvoke(stu, 3, EndOnStudy, stu);
                            //입력매개 변수 타입과 갯수만큼 앞에 넣어주고 다끝나고 동작할 함수 EndOnStudy를 등록한다.
 34                 }
 35             }
 36         }
 37 
 38         public void StudyOn(Stu s,int cnt)
 39         {
 40             //Console.WriteLine("{0} 번 학생 {1} 시간째 공부중", s.Name, cnt);
 41             s.Study(cnt);
 42         }
 43 
 44         public void EndOnStudy(IAsyncResult iar)
 45         {
 46             AsyncResult ar = iar as AsyncResult;
 47             Stu st = ar.AsyncState as Stu;
 48 
 49             OnStudy os = ar.AsyncDelegate as OnStudy;
 50             os.EndInvoke(iar);
 51             if (SchoolCallEventHandler != null)
 52             {
 53                 SchoolCallEventHandler(st); // 전에 등록한 이번트를 호출하여 학생을 되돌려주고 지워준다
 54              list.Remove(st);
 55             }
 56         }
 57     }
 58 }
 59 


'Programing > C#&.Net' 카테고리의 다른 글

명시적 어셈블리 로딩  (0) 2016.11.30
인덱서(Indexer) 예제  (0) 2016.11.30
리플렉션 활용  (0) 2016.11.30
리플렉션(reflection)  (0) 2016.11.30
어셈블리,메타데이터  (0) 2016.11.30

+ Recent posts