using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Thread_Test
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void button_start_Click(object sender, EventArgs e)
        {
            Thread T1 = new Thread(M1);
            Thread T2 = new Thread(M2);

            CheckForIllegalCrossThreadCalls = false; //크로스쓰레드 체크 옵션을 꺼주거나

            T1.Start();
            T2.Start();
        }

        private void M1()
        {
            for (int i = 0; i < 10000; i++)
                listBox_output.Items.Add("Thread1");
                //this.Invoke(new MethodInvoker(delegate { this.listBox_output.Items.Add("Thread1"); })); 이 방법처럼 invoke를 이용한다
        }

        private void M2()
        {
            for (int i = 0; i < 10000; i++)
                listBox_output.Items.Add("Thread2");
                //this.Invoke(new MethodInvoker(delegate { this.listBox_output.Items.Add("Thread2"); }));
        }
    }
}

크로스쓰레트 체크 옵션을 false로 변경할 경우 컨트롤들의 동작이 Visual Studio 2003의 동작방식과 동일해진다고 하는데 세세한 내용은 모르겠음

메인폼(메인쓰레드)에서 생성한 컨트롤들을 다른 신규 쓰레드들에서 접근하려 하면 크로스쓰레드 발생

새로운 규칙에 맞게 가능하면 컨트롤들은 get(), set()등을 이용하여 별도로 접근(음... 뭔가 더욱 객체지향적으로 가겠다는건가...)하는것이 좋겠다

+ Recent posts