Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9166493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:09:54+00:00 2026-06-17T15:09:54+00:00

If I run UIView.StringSize from within some async code like a Task.ContinueWith , it

  • 0

If I run UIView.StringSize from within some async code like a Task.ContinueWith, it will blow up with a UIKitThreadAccessException because that method starts with a call to UIApplication.EnsureUIThread ("Go to declaration" in MonoDevelop; I’m not sure the license allows me to post it here).

Task.Factory.StartNew(() => {
    // Blows up discretely...nothing written to application output.
    // Also blows up for UIViews that exist only in code (not rendered).
    SizeF textSize = someView.StringSize(someString, someFont, new SizeF(someView.Bounds.Width, float.MaxValue), UILineBreakMode.WordWrap);
    Console.WriteLine(textSize);
});

If I wrap this simplified version in InvokeOnMainThread, all is well, but I definitely have times when I want to measure some text without that call. As well, I fully understand the exception’s purpose and it has saved me a bunch of hassle when called something deep within async code before, but in this case the use of EnsureUIThread here seems unnecessary. If I simply restate that call as a hit on the NSString class, it will happily run outside the UI thread.

Task.Factory.StartNew(() => {
    // Outputs expected size data: "{Width=##, Height=##}".
    using (NSString nssSomeString = new NSString(someString)) {
        SizeF textSize = nssSomeString.StringSize(someFont, new SizeF(someView.Bounds.Width, float.MaxValue), UILineBreakMode.WordWrap);
        Console.WriteLine(textSize);
    }
});

The code for UIView.StringSize appears to do roughly the same NSString work and there doesn’t appear to be anything blatantly UI-thread-oriented. Is there something I am missing that would require this version of the method to be called from the UI thread?

Edit (2013-01-17):

I filed a bug with Xamarin just to see their response. It sounds like they are looking into marking this method as ThreadSafe.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:09:55+00:00Added an answer on June 17, 2026 at 3:09 pm

    The UI restriction on the StringSize method is indeed unnecessary. Xamarin has already fixed this for “v6.0.10+”.

    We already detremined that DrawString (drawInRect:* selectors) were fine from
    other threads. StringSize is a subset of that feature so it should be safe to.
    Future versions (6.0.10+) will be marked as [ThreadSafe].

    Until you are coding against the updated version, the NSString version will work just fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run the code from the answer at the UIView vertical
If I drop a UIDatePicker onto a UIView in IB and run the app,
I run this code here <html> <script type=text/javascript src=lib/jquery-ui-1.8.21.custom.min.js></script> <script src=http://127.0.0.1:5984/_utils/script/jquery.couch.js></script> <!--<script type=text/javascript src=lib/jquery-1.7.2.js></script>-->
In my project most of my code is in my UIView subclass. In this
I am receiving crash reports that appear to be from a UIView animation calling
I have been playing with some of Apple's example code for customizing UITableViewCells. I
I have a recursive UIView animatewith duration: delegate to run an animation. When the
I have made two UITableView in a UIView controller and it run smoothly. I
so I have a UIImageView in a UIView but when I run it in
At run-time, in objective-c, how to place a UIObject into a UIView ? Thank

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.