If your code is running in worker thread then you're not allowed to call GetChildAtPoint
or set Location
in it. You need to pass the control to UI thread.
if(cardPanelOpponent.InvokeRequired)
{
cardPanelOpponent.Invoke(new Action(() =>
{
if(cardPanelOpponent.GetChildAtPoint(new Point(i, x)) == null)
{
OpponentCard.Location = new Point(i, x);
cardPanelOpponent.Controls.Add(OpponentCard);
}
});
}
Note: Semantics has changed in above code. We can't add break statement here. So you may need to correct it as your needs.