That looks ok, where is the Taskdialog you mentioned showing from?
Essentially the design pattern I use for modal windows/forms is:
Show the window modal (ShowDialog) this window would be sparse of code essentially just containing controls for data binding of options etc. Whilst this window is showing you should not be able to access Revit (as a user) and would not show TaskDialogs whilst your addin window is showing.
After closing the window you read the control values from it to run your routine calling API members
After that finishes you return status as you have done (end of IExternalCommand Context).
You can reshow modal windows within the context but should close them when calling API members.
So I don't see anything wrong with what you've done except perhaps you may be trying to interact with Revit within the window code behind rather than after you hide the window or before you show it modal?
In principle you should avoid putting API code into window code behind as separation from UI will make it easier to convert to Forge in the future (where you can't show UI).
Often the only code I have in window code behind is handler for ok button that sets a OkPressed boolean to true and closes the window. I check this OKPressed from outside the window after it closes to check if I continue or not i.e. any other form of closing the window (X button/escape etc.) should Return Result.Cancelled from the IExternalCommand.