diff --git a/WPFExample/MainWindow.xaml.cs b/WPFExample/MainWindow.xaml.cs index 7472837..b243fc5 100644 --- a/WPFExample/MainWindow.xaml.cs +++ b/WPFExample/MainWindow.xaml.cs @@ -21,6 +21,8 @@ public partial class MainWindow : Window private Finsemble FSBL; + private bool finsembleRequestedClose = false; + private void SpawnComponent_Click(object sender, RoutedEventArgs e) { string componentName = ComponentSelect.SelectedValue.ToString(); @@ -64,6 +66,7 @@ public MainWindow(string[] args) FSBL.Connect(); } + private void Finsemble_Connected(object sender, EventArgs e) { Application.Current.Dispatcher.Invoke(delegate //main thread @@ -191,6 +194,39 @@ private void Finsemble_Connected(object sender, EventArgs e) }); */ + //listen for window close requests from FInsemble so we can differentiate user and system close requests + /*string closeTopic = "WindowService-Event-" + FSBL.windowName + "-close-requested"; + FSBL.RouterClient.AddListener(closeTopic, (s, args) => + { + finsembleRequestedCloseAt = DateTime.UtcNow; + });*/ + + //wait for the window to come up + FSBL.RouterClient.AddListener("WindowService-Event-" + FSBL.windowName + "-ready", (t, arguments) => + { + //Register an event listener to pause Finsemble during workspace switch/shutdown/restart events until this listener responds + var guid = DateTime.UtcNow + " " + FSBL.windowName; + FSBL.RouterClient.Query("WindowService-Request-addEventListener", new JObject + { + ["windowIdentifier"] = FSBL.WindowClient.windowIdentifier, + ["eventName"] = "close-requested", + ["guid"] = guid + }, (s, args) => { + + }); + + //When a close-requested event fires, log the timestamp abd then publish on the interrupt channel to let FInsemble know it can continue + string closeTopic = "WindowService-Event-" + FSBL.windowName + "-close-requested"; + FSBL.RouterClient.AddListener(closeTopic, (s, args) => + { + finsembleRequestedClose = true; + FSBL.RouterClient.Publish("Finsemble.Event.Interrupt." + guid, new JObject + { + ["delayed"] = false + }); + }); + }); + } /// @@ -200,13 +236,27 @@ private void Finsemble_Connected(object sender, EventArgs e) /// private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { - /*if (MessageBox.Show("Close Application?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No) - { - // Cancel Closing - e.Cancel = true; - return; - }*/ - } + //consider yielding here to let the message arrive? + if (finsembleRequestedClose) + { + finsembleRequestedClose = false; + if (MessageBox.Show("Finsemble is requesting that this app close, proceed?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No) + { + // Cancel Closing + e.Cancel = true; + return; + } + } + else + { + if (MessageBox.Show("Close Application?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No) + { + // Cancel Closing + e.Cancel = true; + return; + } + } + } private void LinkToGroup_Click(object sender, RoutedEventArgs e) {