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...