As part of testing my app under AWS's AppStream environment, I've run into an exception that gets thrown when I interact with the taskbar (in my case, to set a custom tooltip). I've been able to reproduce it in a blank WPF app with this kind of code:
private void Button_Click(object sender, RoutedEventArgs e)
{
TaskbarItemInfo = new System.Windows.Shell.TaskbarItemInfo { Description = DateTime.Now.ToLongTimeString() };
}
Running that code in AppStream gives a NotImplementedException
with this stack trace:
at MS.Internal.AppModel.ITaskbarList.HrInit()
at System.Windows.Window.ApplyTaskbarItemInfo()
at System.Windows.Window.OnTaskbarItemInfoChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.Window.<>c.<.cctor>b__0_0(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
at My.App.MainWindow.Button_Click()
I found this post that I think is the same underlying problem: ITaskbar HrInit method throws exception under RemoteApp. Interestingly enough, I've tested the app in RemoteApp and it works fine there. I'm guessing Microsoft has improved RemoteApp since then to avoid this problem on their platform.
It seems like the exception occurs when Explorer is not running - which makes sense since it's hard to talk to the taskbar when it doesn't exist. In fact, I've been able to reproduce this same issue by just killing explorer.exe.
The only potential fix mentioned in that other question was to look at SystemInformation.TerminalServerSession
to detect when you are running in this kind of environment and avoid using TaskbarItemInfo
. That doesn't work for me because that will also detect a normal Remote Desktop connection - which is a very common use case for my app and a situation where the code works fine.
The only other thing I could think of would be to wrap the line of code in a try/catch block and just blindly swallow whatever may go wrong. That's not the end of the world since the app will behave just fine without my custom taskbar tooltip logic. But that just feels....wrong. It almost feels like this is the kind of error handling that WPF should be doing. I know not having explorer.exe running is an odd thing but I've heard of people using alternate shells or (perhaps more likely), seen explorer.exe crash from time to time.