12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 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;
- namespace BEMSDataGateway
- {
- delegate void TestDelegate(string msg);
- delegate void TestDelegate2();
- public partial class Loading_Form : Form
- {
- public Loading_Form()
- {
- InitializeComponent();
- System.Threading.Thread thread = new System.Threading.Thread(Thread1);
- thread.Start();
- }
- private void Loading_Form_Load(object sender, EventArgs e)
- {
- }
- private void showText(string msg)
- {
- Loading_Point.Text = msg;
- }
- private void Thread1()
- {
- for (int i = 0; i < 101; i++)
- {
- //[증상]
- //Control.Invoke()를 이용하여 서로 다른 스레드의 컨트롤에 접근하고자 할때, 간헐적으로 아래에 예외가 발생할 수 있습니다.
- //[예외]
- //MainForm.WorkerThreadFunction|System.InvalidOperationException: 창 핸들을 만들기 전까지 컨트롤에서 Invoke 또는 BeginInvoke를 호출할 수 없습니다.
- //(Invoke or BeginInvoke cannot be called on a control until the window handle has been created)
- //[원인]
- //예외 메시지 그대로 핸들이 만들어 지기 전에 Invoke 메서드가 호출되기 때문.
- //[해결 방안]
- //아래와 같이 명시적으로 핸들 값을 변수에 할당함으로써, 이렇게 하면 내부적으로 핸들이 없는 경우 강제로 만들어 지도록 한 후에 Invoke를 호출한다.
- ForceToCreateHandle();
- this.Invoke(new TestDelegate(showText), i.ToString());
- System.Threading.Thread.Sleep(20);
- }
- this.Invoke(new TestDelegate2(Close));
- }
- private void ForceToCreateHandle()
- {
- IntPtr x;
- if (!this.IsHandleCreated)
- x = this.Handle;
- }
- }
- }
|