'분류 전체보기'에 해당되는 글 157건

  1. 2009.01.24 서로 다른 클레스와 event를 이용한 통신
  2. 2009.01.24 델리게이트와 이벤트
  3. 2009.01.24 Thread 동기화 Collection류

서로 다른 클레스와 event를 이용한 통신

|

// 하나의 test폼 클레스.. 

 

namespace EventTestForm1
{
    public partial class Form1 : Form
    {
        TestEvent test;
        public Form1()
        {
            InitializeComponent();
            test = new TestEvent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            test.Message = this.textBox1.Text;
            test.gogo += new TransFrom.TransFrom(OnTextBox);//이벤트 등록
            test.EventGo();
        }
        public void OnTextBox(object sender, EventArgs e)//호출되는 함수
        {
            TestEvent ts = (TestEvent)sender;
            this.textBox1.Text = ts.Message;
        }
    }
}

namespace TransFrom
{
    public delegate void TransFrom(object sender, EventArgs e);

    public class TestEvent
    {
        public event TransFrom gogo;
        public string sMsg;
        public string Message
        {
            get
            {
                return sMsg;
            }
            set
            {
                sMsg = value;
            }
        }
        public TestEvent()
        {
        }
        public void EventGo()
        {
            EventArgs e = new EventArgs();
            sMsg += "AAAAAAAAAA";
            gogo(this, e);//이벤트 발생.. 등록되어있는 이벤트함수 실행
        }
    }
}

And

델리게이트와 이벤트

|

사용 하는 방법만

 간단히 보자..


namespace CSharpExample
{
    public delegate void Defeat();//이벤트 등록을위한 델리게이트
    public class Zealot
    {
        public void Attack()
        {
            Console.WriteLine("Zealot Attacked !!");
        }
    }
    public class Dragon
    {
        public void Attack()
        {
            Console.WriteLine("Dragon Attacked !!");
        }
    }
    public class Carrier
    {
        public void Attack()
        {
            Console.WriteLine("Carrier intercepter Attacked !!");
        }
    }
    public class Group
    {
        private event Defeat EnemyDetected; //이벤트 델리게이트 필드

        public void AddCombatUnit(Defeat unit)
        {
            EnemyDetected += unit;//이벤트시 실행 함수 추가 부분
        }
        public void AttackEnemy()
        {
            EnemyDetected();//이벤트 실행.. 등록되있는 모든 함수 실행한다
        }//만약에 중간에 예외처리로 thorw된다면 그 다음 이벤트는 수행 되지 않음
    }
    public class Starcraft4
    {
        [STAThread]
        public static void Main()
        {
            Zealot unit10 = new Zealot();
            Dragon unit20 = new Dragon();
            Carrier unit30 = new Carrier();

            Group group1 = new Group();

            group1.AddCombatUnit(new Defeat(unit10.Attack));//이벤트에 추가
            group1.AddCombatUnit(new Defeat(unit20.Attack));//이벤트에 추가
            group1.AddCombatUnit(new Defeat(unit30.Attack));//이벤트에 추가

            group1.AttackEnemy();//이벤트 발생(실행한다)
        }
    }
}

And

Thread 동기화 Collection류

|

Collection 에서 제공하는 SyncRoot이용


사용예)


public class ICollectionSync{
 private Int32[ ] intArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 public void SyncRun(){
  ICollection c = intArr;
  lock(c.SyncRoot){
   IEnumerator e = c.GetEnumerator();
   while(e.MoveNext()){
    Console.Write((int)e.Current +"t");
    try{
     Thread.Sleep(20);
    }catch{ }
   }
   Console.WriteLine("요소갯수:{0} ", c.Count);
  }//lock
 }
}//class

And
prev | 1 | ··· | 48 | 49 | 50 | 51 | 52 | 53 | next