• After more than 30 years running websites and forums I am retiring.

    I have made many friends through the years. I will cherish my time getting to know you. I wish you all the best. This was not an easy decision to make. The cost to keep the communities running has gotten to the point where it's just too expensive. Security certificates, hosting cost, software renewals and everything else has increased threefold. While costs are up ad revenue is down. It's no longer viable to keep things running.

    All sites will be turned off on Thursday 30 November 2023. If you are interested in acquiring any of the websites I own you can Email Schwarz Network.

Render FrameworkElement (F#) without outside WPF application

  • Thread starter This_display_name_is_already_in_use_They_all_are
  • Start date


Original Text from WPF Forum: (Still relevant)

We have a Class, written in F# which is basically a FrameworkElement. Internal there is a VisualCollection which holds the rendered parts - for compositioning i guess.

The class itself represent a Graphic-To-Be-Rendered, which in this special case, has to be done without a UI thread and outside of a WPF application.

What we do is the following:

  1. Spawn an STA Thread
  2. - Instantiate the Graphics
  3. - Populate with Data
  4. - Render to Bitmap
  5. - Return Synchronized stream
  6. Outside the STA Thread
  7. - Write stream to File

The file is empty but has the correct width & length.

I believe that it is not possible to do this without an App() or a WPF Context of some kind. We're kind of at a loss here.

Any Suggestions are welcome, since i've got nothing a the moment. The goal is basically to render something WPF into a FileStream from withing a thread inside a console application.

F# related addendum:

With combinding some of the solutions i got it working - but with a hack i cannot use in production. But at least now we know a little bit more about the Problem.

In F#, whenever we calculate stuff, we run

do! Async.SwitchToThreadPool()

When we are ready for actually drawing stuff, we run

do! Async.SwitchToContext guiSynchronizationContext

When debugging, i can get as far as to switching back to the UI Context, but there the execution stops - or the Debugger cannot hit any further breakpoint.

No Exception, no Debug output - nothing to work with. The rendered output is empty.

Now if we replace all the SwitchToThreadPool commands with SwitchToUIContext ones, the rendering suddenly starts working. Meaning that there is some kind of configuration issue that prevents us from reliably switching between the STA thread and the thread pool.

Continue reading...
Top Bottom