Search code examples
wpfprism

Prism WPF CompositeCommand issues


I have an application where I am using a CompositeCommand to fire off a series of child commands. The child commands combined could take upward of 15 seconds to finish. While that's happening I simply want to display a message to the user. Actually it would be nice if I could display progress, but just displaying the message itself is proving annoyingly difficult. I tried using DialogService to show a dialog but that didn't work because I couldn't figure out a way to close it after the child commands finished. So I thought I'd put a message covering everything on my view and bind its Visibility property to an InProgress property. The button on screen runs a regular Delegate command that performs:

private void ShowInProgressDialog()
{
   InProgress = true;
   _applicationCommands.RunFullScanCommand.Execute(null);
}

And I use EventAggregator to send messages from each child to say they're done, setting InProgress back to false. Stepping through, everything hits its breakpoint, but the composite command does not allow the UI to be updated until the child commands are all complete. So even though the InProgress property gets set properly throughout the process, the UI never gets updated and never shows my message UI. So I tried breaking it out into its own method:

private void ShowInProgressDialog()
{
   InProgress = true;
}

private void RunScan()
{
   ShowInProgressDialog();
   _applicationCommands.RunFullScanCommand.Execute(null);
}

Still same result. OK, then, what if I try something hacky like publishing a message to myself and subscribing to that. So, in effect, the code attached to the delegate command will totally finish running after publishing its message and then the view model separately receives the message and fires off the CompositeCommand:

private void ShowInProgressDialog()
{
   InProgress = true;
}

private void RunScan()
{
   ShowInProgressDialog();
   _eventAggregator.GetEvent<ScanBeginEvent>().Publish(new Scan(ScanType.Full, ScanFunction.System));
}

private void ScanBegin(Scan scan)
{
   _applicationCommands.RunFullScanCommand.Execute(null);
}

Nope. Same result. This seems like something that should be doable but I'm running into walls here and running out of ideas. Anyone else do anything like this and get it working?


Solution

  • Turns out I did have to move some processing into a Task. The above behavior I described was with everything on the UI thread. As mentioned in Haukinger's comment, it was blocking. I had also previously tried moving things into a Task (forgot to mention that in my original post) but that was not performing my navigation properly when the results came back. Essentially, I was trying to put too much processing into the Tasks. Once I limited it to only what was absolutely necessary in the Tasks, it worked properly.