Use a maybe monad to be able abort an analysis.
[master-thesis.git] / Parasitemia / ParasitemiaUI / Analysis.fs
index 24f9adb..9b9ed51 100644 (file)
@@ -117,30 +117,34 @@ let showWindow (parent: Window) (state: State.State) : bool =
             then
                 MessageBox.Show("No image selected", "Cannot start analysis", MessageBoxButton.OK, MessageBoxImage.Information) |> ignore
             else
+                stackSourceImagesSelection.IsEnabled <- false
                 analysisPerformed <- false
                 butStart.IsEnabled <- false
                 butClose.Content <- "Abort"
+
                 async {
-                    let results =
+                    let maybeResults =
                         ParasitemiaCore.Analysis.doMultipleAnalysis
                             imagesToProcess
-                            (Some (fun progress -> window.Root.Dispatcher.Invoke(fun () -> progressBar.Value <- float progress)))
+                            (Some (fun progress -> window.Root.Dispatcher.Invoke(fun () -> progressBar.Value <- float progress); not analysisCancelled))
 
                     lock monitor (
                         fun() ->
-                            if not analysisCancelled
-                            then
+                            match maybeResults with
+                            | Some results ->
                                 for id, cells in results do
                                     state.SetResult (int id) cells
 
                                 window.Root.Dispatcher.Invoke(fun () ->
+                                    stackSourceImagesSelection.IsEnabled <- true
                                     butStart.IsEnabled <- true
                                     butClose.Content <- "Close"
                                     updateSourceImages ())
 
                                 Logger.Log.User("All analyses terminated successfully")
                                 atLeastOneAnalysisPerformed <- true
-                                analysisPerformed <- true)
+                                analysisPerformed <- true
+                            | None -> ())
                 } |> Async.Start
         | _ -> ())