[Thread Invoke / BeginInvoke] invoke 와 begininvoke 의 차이 연구


Created with colorer-take5 library. Type 'csharp'

/*
 * Created by SharpDevelop.
 * User: jjongun
 * Date: 2010-03-04
 * Time: ��전 11:27
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
namespace TestEventThread
{
    //CrossThread 를 위한 Delegate
    public delegate void guideThreadCallBack(string str);
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            
            Trace.WriteLine("startThread    " + Thread.CurrentThread.ManagedThreadId);
            //새로운 Thread 선언 
            testThread = new Thread( new ThreadStart(inThread));
            testThread.Start();
            
            //종료를 위한 이벤트
            this.Disposed += new EventHandler(MainForm_Disposed);
            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
        }

        void Application_ApplicationExit(object sender, EventArgs e)
        {
            Trace.WriteLine("Application_ApplicationExit");
        }
        
        void MainForm_Disposed(object sender, EventArgs e)
        {
            Trace.WriteLine("MainForm_Disposed");
            testThread.Abort();
        }
        
        //Thread Target
        void inThread()
        {
            for(int i =0; i <1000 ; i++)
            {
                crossThread("Test String Number : " + i.ToString());
                Trace.WriteLine("SleepThread    " + Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(1000);
                
            }
        }
        
        //Invoke  or  BegiInvoke
        void crossThread(string str)
        {
            Trace.WriteLine(str +"   currentThread " +Thread.CurrentThread.ManagedThreadId.ToString());
            if(this.InvokeRequired)
            {
                Trace.WriteLine("    InvokeRequired " + Thread.CurrentThread.ManagedThreadId.ToString() + "\n\n\n");
                
                this.Invoke(new guideThreadCallBack(crossThread) , new Object[]{str});
                //this.BeginInvoke(new guideThreadCallBack(crossThread) , new Object[]{str});
            }else{
                this.Text = str ;
                Trace.WriteLine("NOT InvokeRequired " + Thread.CurrentThread.ManagedThreadId.ToString());
            }
        }
        
        private Thread testThread;
    }
}
 
 
 
 
 
위의 코의에서 this.invoke 와 this.beginInvoke 를 비교하고 있다. 여기서 Trace 를 보면 



와 같이 차이를 보인다.

SleepThread 라고 적어놓은 부분이 발생하는 순서가 다르다.

이유는 현제 모름
Yamecoder 야매코더_
C# 2010.12.04 02:15
Powerd by Tistory, designed by criuce
rss