Computers/Language 2012. 5. 1. 19:54

스레드로부터 안전한 방식으로 Windows Forms 컨트롤 호출

http://msdn.microsoft.com/ko-kr/library/ms171728%28VS.80%29.aspx


http://sanbj.tistory.com/entry/CCross-Thread


아래 오류에 대한 대처법


크로스 스레드 작업이 잘못되었습니다. 

'richTextBox1' 컨트롤이 자신이 만들어진 스레드가 아닌 스레드에서 액세스되었습니다.


===========================================

    public partial class Form1 : Form

    {

        DataSet ds = new DataSet();

        OleDbDataAdapter adaptor = new OleDbDataAdapter();

        delegate void SetTextCallback(string text);

        public Form1()


----------------------------------------------------------------


        // Data received from COM port

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)

        {

            this.Invoke(new EventHandler(SerialReceived));

        }

        private void SerialReceived(object s, EventArgs e)

        {

            string text;

            text = serialPort1.ReadExisting();

            if (text != null)

            {   //데이터 처리하기 richTextBox1.Text += buff;

                SetText(text);

            }

        }

        // To avoid Cross thread error -----------------------------------------

        // http://msdn.microsoft.com/ko-kr/library/ms171728%28VS.80%29.aspx

        private void SetText(string text)

        {

            if (this.richTextBox1.InvokeRequired)

            {

                SetTextCallback d = new SetTextCallback(SetText);

                this.Invoke(d, new object[] { text });

            }

            else

            {

                this.richTextBox1.Text += text;

            }

        }

        // --------------- End COM port data handling --------------------------


posted by 털보네i
: