I have an application that requires a user to enter SQL connection information in a dialog. When the user clicks the "Connect" button, I await
the result of SqlConnection.OpenAsync()
with the supplied credentials in order to test that they are valid before closing the dialog box. I also disable the Connect button to avoid duplicate attempts. When the credentials are correct, it returns almost immediately, but when they are not, it can take up to 30 seconds to return. The cancel button is able to use a CancellationToken
to cancel the request and close the dialog.
The problem is that the user is still able to click the window's close button on the dialog to dismiss it. My view model doesn't get notified, but the form is still closed. 30 seconds or so later the connection attempt returns with error information, and shows a message box.
Is there a good, MVVM friendly way, to cancel the connection attempt with my CancelationToken
when the form is closed in this way? I know that I could rig up something using in the dialog's code-behind, but I'd like to avoid referencing the view model from there.