Skip to content

Commit

Permalink
Add helpers for threading
Browse files Browse the repository at this point in the history
  • Loading branch information
marner2 committed Apr 23, 2024
1 parent fd8a645 commit 91a2893
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Elmish.WPF/WpfProgram.fs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,30 @@ module WpfProgram =
Application () |> ignore
Application.Current.MainWindow <- window

let runElmishLoopOnBackgroundThread window program =

let elmishThread =
System.Threading.Thread(
System.Threading.ThreadStart(fun () ->
startElmishLoop window program
System.Windows.Threading.Dispatcher.Run()))
elmishThread.Name <- "ElmishDispatchThread"
elmishThread.Start()

elmishThread

let shutdownElmishBackgroundThread thread =
System.Windows.Threading.Dispatcher.FromThread(thread).InvokeShutdown()
thread.Join()

let runWindowThreaded window program =
initializeApplication window
let elmishThread = runElmishLoopOnBackgroundThread window program
window.Show ()
let exitCode = Application.Current.Run window
shutdownElmishBackgroundThread elmishThread
exitCode


/// Starts the Elmish and WPF dispatch loops. Will instantiate Application and set its
/// MainWindow if it is not already running, and then run the specified window. This is a
Expand Down

0 comments on commit 91a2893

Please sign in to comment.