This project is read-only.

Small SilverSprite bug (image)

Oct 31, 2009 at 5:14 PM

Here is a link to an image that shows the entire exception information.

http://cid-ed3ec499646d9ba3.skydrive.live.com/self.aspx/.Public/InvalidCrossThreadAccess.png

I don't get this error in XNA but I do get it in SilverSprite. I get it when I try to call MeasureString on a SpriteFont from within a separate thread.

private void ....
{
....
// get a list of classes to allow
// the user to select
NetworkAbstractionLayer.GetClasses(((a) =>
{
_classes = (List<string>)a;

generateClassButtons();

_classesLoaded = true;
determineLoadingFinished();
}));
....
}

// generates a row of buttons across the top
// that allows the user to select a class.
private void generateClassButtons()
{
// calculate the left-most word's position
// (center in screen)
int spacer = 50;
int left = ((int)DefaultFont.MeasureString(string.Join(string.Empty, _classes.ToArray())).X) + (spacer * (_classes.Count - 1));
left = ((int)ScreenCenter.X - (left / 2));
bool firstButton = true;
...........
}

Oct 31, 2009 at 5:48 PM

If you're interested in a potential fix:

public Vector2 MeasureString(string text)
{
    if (this is BitmapSpriteFont)
    {
        return ((BitmapSpriteFont)this).InternalMeasureString(text);
    }
    if (text == null) text = "";
    if (textSizes.ContainsKey(text))
    {
        return textSizes[text];
    }

    Vector2 size = Vector2.Zero;

    if (tb.CheckAccess())
    {
        tb.Text = text;
        size = new Vector2((float)tb.ActualWidth, (float)tb.ActualHeight);
    }
    else
    {
        tb.Dispatcher.BeginInvoke(() =>
        {
            tb.Text = text;
            size = new Vector2((float)tb.ActualWidth, (float)tb.ActualHeight);
        });
    }

    textSizes.Add(text, size);
    return size;
}

Nov 14, 2009 at 5:38 PM

Thanks this will be incorporated in the next release, I plan on having an alpha release soon.