관리되는 컨트롤이 삭제되어 더 이상 핸들이 없지만 아직 창을 닫지 않아 아직 볼 수 없기 때문에 이러한 현상이 발생합니다.
Invoke
전화를 걸고 Invoke
을 호출하기 전에 IsHandleCreated
을 확인하는 함수를 만들 수 있습니다. IsHandleCreated
이 거짓이면 Invoke
으로 전화 할 수 없으며 전화를 무시하면됩니다.
public static class ControlExtensions
{
public static TResult InvokeEx<TControl, TResult>(this TControl control,
Func<TControl, TResult> func)
where TControl : Control
{
if (!control.IsHandleCreated)
return default(T);
if (control.InvokeRequired)
return (TResult)control.Invoke(func, control);
else
return func(control);
}
public static void InvokeEx<TControl>(this TControl control,
Action<TControl> action)
where TControl : Control
{
control.InvokeEx(c => { action(c); return c; });
}
}
는 이제 단지
InvokeEx
와 스레드에서 모든
Invoke
의 포장 및 컨트롤이 배치되고 더 이상 핸들이되었을 때 그것을 처리합니다.
this.InvokeEx(c => c.label1.Text = "Hello world");