■ 복수 UI 스레드를 만드는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 부모 설정하기 - SetParent(childWindowHandle, parentWindowHandle) /// <summary> /// 부모 설정하기 /// </summary> /// <param name="childWindowHandle">자식 윈도우 핸들</param> /// <param name="parentWindowHandle">부모 윈도우 핸들</param> /// <returns>처리 결과</returns> [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr SetParent(IntPtr childWindowHandle, IntPtr parentWindowHandle); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 열 카운트 /// </summary> private const int COLUMN_COUNT = 5; /// <summary> /// 행 카운트 /// </summary> private const int ROW_COUNT = 5; /// <summary> /// 폼 리스트 /// </summary> private List<PopupForm> formList = new List<PopupForm>(); /// <summary> /// 난수기 /// </summary> private Random random = new Random(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); #region 복수 UI 스레드 사용하기 체크 박스를 설정한다. this.useMultiUIThreadCheckBox.Checked = true; #endregion #region 데이터 그리드 뷰를 설정한다. ControlHelper.SetDoubleBuffering(this.dataGridView, true); for(int i = 0; i < COLUMN_COUNT; i++) { this.dataGridView.Columns.Add(i.ToString(), i.ToString()); } this.dataGridView.RowCount = ROW_COUNT; #endregion #region 이벤트를 설정한다. FormClosing += Form_FormClosing; this.createButton.Click += createButton_Click; this.timer.Tick += timer_Tick; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼을 닫을 경우 처리하기 - Form_FormClosing(sender, e) /// <summary> /// 폼을 닫을 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_FormClosing(object sender, FormClosingEventArgs e) { foreach(PopupForm form in this.formList) { try { form.CloseForm(); } catch { } } this.formList.Clear(); } #endregion #region 팝업 폼 생성하기 버튼 클릭시 처리하기 - createButton_Click(sender, e) /// <summary> /// 팝업 폼 생성하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void createButton_Click(object sender, EventArgs e) { for(int i = 0; i < 20; i++) { if(this.useMultiUIThreadCheckBox.Checked) { AddPopupFormWithUIThread(); } else { AddPopupForm(); } } } #endregion #region 타이머 틱 처리하기 - timer_Tick(sender, e) /// <summary> /// 타이머 틱 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void timer_Tick(object sender, EventArgs e) { for(int i = 0; i < COLUMN_COUNT; i++) { for(int j = 0; j < ROW_COUNT; j++) { this.dataGridView.Rows[j].Cells[i].Value = this.random.Next(0, 100).ToString(); } } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 팝업 폼 추가하기 - AddPopupForm() /// <summary> /// 팝업 폼 추가하기 /// </summary> private void AddPopupForm() { PopupForm form = new PopupForm(); this.formList.Add(form); form.Show(this); SetParent(form.Handle, Handle); } #endregion #region UI 스레드를 갖는 팝업 폼 추가하기 - AddPopupFormWithUIThread() /// <summary> /// UI 스레드를 갖는 팝업 폼 추가하기 /// </summary> private void AddPopupFormWithUIThread() { PopupForm form = null; Thread thread = new Thread(() => { form = new PopupForm(); this.formList.Add(form); Application.Run(form); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); while(true) { if(form != null && form.IsHandleCreated == true) { break; } } IntPtr handle = Handle; form.Invoke(new Action(delegate { SetParent(form.Handle, handle); })); } #endregion } } |