I have a question about what should and shouldn't be done cross-threads. Essentially I want to know if it is safe to change values created in one thread from another thread. Consider my following code:
I know it might be confusing to know what's going on, but _cards was created in the main thread and many properties accessed within the MoveCard method were also created in the main thread.Code:public void AddCardToAnimate(Card aCard, int aNewX, int aNewY, bool aFlip)
{
aCard.SetAnimationValues(aNewX, aNewY, aFlip);
_cards.Add(aCard);
if(_cards.Count == 1)
{
AnimationDelegate ad = new AnimationDelegate(AnimateCards);
ad.BeginInvoke(null, ad);
}
}
private void AnimateCards()
{
// While there are still cards to be animated
while(_cards.Count!=0)
{
// Get the first card in the array
Card animatingCard = (Card)_cards[0];
// Ensure that the card being animated can be seen
animatingCard.Visible = true;
// Animate the card
MoveCard(animatingCard);
// Remove the card from the "to be animated" array
_cards.RemoveAt(0);
}
}
Is this ok? Do I either need to encapsulate all of the properties that are being changed into one thread, or have many delegates?
The program seems to work fine this way...but I just want to know the proper way to go about it, and I'm a bit confused with the whole threading thing.
