[BackGroundWorkerTEST] Sync , Async , Thread





주요 Code




MSDN
http://msdn.microsoft.com/ko-kr/library/system.componentmodel.backgroundworker(v=VS.90).aspx?appId=Dev10IDEF1&l=KO-KR&k=k(EHINVALIDOPERATION.WINFORMS.ILLEGALCROSSTHREADCALL);k(TargetFrameworkMoniker-".NETFRAMEWORK&k=VERSION=V3.5");k(DevLang-CSHARP)&rd=true




Created with colorer-take5 library. Type 'csharp'

//sync
        private void testCodeSync()
        {
            using (System.Net.WebClient wc = new System.Net.WebClient())
            {
                wc.DownloadFile(streamPath, "test_"+checkTime.ToString()+currentState+".tmp");
            }
            stateTx.Text = currentState + " :: done";
        }


        //async
        private void testCodeAsync()
        {
            using (System.Net.WebClient wc = new System.Net.WebClient())
            {
                wc.DownloadFileAsync(streamPath, "test_" + checkTime.ToString() + currentState + ".tmp");
            }
            stateTx.Text = currentState + " :: done";
        }
        

        
        //################
        //backgroundworker
        BackgroundWorker bw = new BackgroundWorker();
        void BGWorker()
        {
            //bw = new BackgroundWorker();
            bw.RunWorkerAsync();
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        }
        

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //RunWorkerCompleted 이벤트 안에서는 윈폼 스레드로 돌아와 안전��게 컨트롤 할�� 있다.
            stateTx.Text = currentState + " :: done";
            bw.Dispose();
        }

        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            using (System.Net.WebClient wc = new System.Net.WebClient())
            {
                wc.DownloadFile(streamPath, "test_" + checkTime.ToString() + currentState + ".tmp");
            }
            //에러! : 크로스 스레딩 ( 스레드가 다르다 )
            //stateTx.Text = currentState + " :: done";
        }
인터넷 속도가 빠를경우 테스트가 잘 되지 않지만 Async 도 처음에 실행할때는 약간의 딜레이가 생긴다
sync 는 항상 딜레이가 생긴다.
테스트 타겟은 구글의 메인페이지 이다.
Yamecoder 야매코더_
C# 2010.12.04 02:26
Powerd by Tistory, designed by criuce
rss