본문 바로가기

C# Windows Forms

[Windows Form] Invoke를 사용하여 UI Thread의 대리자를 통한 크로스 쓰레드 처리

Invoke(Action)을 활용하여  크로스 쓰레드 처리 방법

        void LabelSetText(Label lbtemp, string stemp)
        {
            if (lbtemp.InvokeRequired)
            {
                lbtemp.Invoke(new Action<Label, string>(LabelSetText),lbtemp,stemp));
            }
            else
            {
                lbtemp.Text = stemp;
            }
        }

Invoke를 사용하면 편하지만, Await을 통한 UI 쓰레드 동기 처리를 하는 방식이 더 깔끔하다.