开发者

Silverlight - Is it possible to create UI elements on a background thread?

I'm building a line chart control in Silverlight using PolyLineSegment and points. It works just as expected, but the application freezes for a long time when there's too much data that needs to be visualized (too many points). I can't move my code on a separate thread for an obvious reason - it deals with UI elements directly, so when开发者_运维知识库 I try to call them from a separate thread it results in exception (even if UI elements are not yet rendered).

Is there any way to create UI elements dynamically on a background thread and then pass them to the UI thread to be rendered? And if not, what would be the possible solution? I'm thinking of creating an Bitmap image instead of actual controls, but there won't be much interactivity in this case.


It sounds like you need to get a faster way of rendering your points. If you have 800k samples and only say, 800 pixels to display them in you're wasting 1000 points per pixel of calculations if you just load it into a PolyLineSegment.

I would revisit 'interpolating' the points (this is really coalescing for your large dataset). You want to make sure you capture the dynamic range of the function in each pixel correctly:

  • Figure out how many pixels wide the graph should be
  • Determine how many points per pixel in the X direction
  • For each chunk of points:
    • Build a histogram of the points
    • Draw a vertical line from max->min on your graph at the X where these points will map to. This captures the full range represented in the chunk.

If your points/pixel gets close to 1 you'll want to switch to the easy rendering to give better visual results as well.


For displaying a waveform (in your case PCM audio data) with "millions of points" you would be better off writing directly to a WritableBitmap. You then have only one render object.

You have already said there is not much processing in your calculations. Trying to use individual UIElements is way too big an overhead (IMHO). Point display is trivial to a bitmap and there are plenty of line drawing algorithms out there, optimised for speed, to do any line segments.

You can plot your points on a background thread and them update an image's ImageSource at the end of the processing to display it.


You certainly can do your compute work on background thread(s) and pass the finished results up to the UI tread with

Deployment.Current.Dispatcher.BeginInvoke

which is discussed here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜