I have a textblock with text and fontsize. I can find the desired size for the textblock using Textblock.Measure(), but this has to be done in the background. Is there a way to compute this in background (Task.Run).
Asked
Active
Viewed 109 times
0
-
1what do you mean when you say background? Could you please elaborate. I do have an answer for you but based on your explanation, I'll be able to answer you more precisely. Please share, what you want to do with the size of the textblock and what does background mean – iam.Carrot Jun 28 '17 at 09:30
-
Be specific, what is the problem if you run `Textblock.Measure` in the background? Do you encounter some kind of exception? or does this call just not return the correct desired size? – kennyzx Jun 28 '17 at 09:40
-
@AdityaSharma I need to compute the TextBlock's DesiredSize inside Task.Run like Task.Run(()=> var MyTextBlock = new TextBlock(); MyTextBlock.Measure()); But if I do this it will throw mashelling exception. – Saravana Kumar Jun 28 '17 at 11:50
-
@kennyzx The problem is if I create a new Textblock and measure it inside Task.Run(()=>{}); It will throw mashelling exception. – Saravana Kumar Jun 28 '17 at 11:52
-
mashelling exception is caused as you're trying to perform an operation that needs to be done on a UI thread in a background thread. You can solve it using a Dispatcher. You can find a solution for it [here] (https://stackoverflow.com/a/27698035/3766231) – iam.Carrot Jun 28 '17 at 11:56
-
Yes but dispathcer will take it back to the UI thread, but I want this to happen all in another thread without disturbing the UI thread. – Saravana Kumar Jun 28 '17 at 11:57
-
Now if you could please elaborate on why do you need the desired size for the textblock I could finalize an answer and provide it to you – iam.Carrot Jun 28 '17 at 11:57
-
I'm trying to create a datagrid. I want to know the maximum width a column requires. In order to compute that I want to know the desired size of all the textblocks in that particular column. – Saravana Kumar Jun 28 '17 at 12:03
-
@SaravanaKumar yes, update this in the question. Provide more details like the exact exception type and message, post screenshots if necessary, and give the code. This is how to ask. – kennyzx Jun 28 '17 at 12:10