private void CheckForCrossThreadAccess()
{
// force handle creation, so InvokeRequired() will use it instead of searching through parent's chain
if (!IsHandleCreated)
{
CreateControl();
// if the control is not Visible, then CreateControl() will not be enough
if (!IsHandleCreated)
{
CreateHandle();
}
}
if (InvokeRequired)
{
throw new InvalidOperationException("Cross thread access to the control is not allowed.");
}
}