20秒ぴったりかかっているので、何かの処理がタイムアウトするまでプロセスが残っているのかも。
まぁ、対症療法が許容されるなら、プログラムの最後に自プロセスを強制的に終わらせればすぐに消えます。
それがダメならwinscard.hにあるAPIを使えばよいかも。(こっちは試してません)
namespace SmartCardTest
{
using System;
public partial class App : System.Windows.Application
{
// App.xamlのビルドアクションをPageに変更しておく
[System.STAThread]
public static void Main()
{
App app = new App();
app.InitializeComponent();
app.Run();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.Diagnostics.Process.GetCurrentProcess()?.Kill();
}
protected override void OnStartup(System.Windows.StartupEventArgs e)
{
base.OnStartup(e);
this.Activated += app_Activated;
if (this.StartupUri == null)
{
var w = new System.Windows.Window() { Content = "Smart Card Test", SizeToContent = System.Windows.SizeToContent.WidthAndHeight };
w.Show();
}
}
private async void app_Activated(object sender, System.EventArgs e)
{
this.Activated -= app_Activated;
await TestAsync();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
await System.Threading.Tasks.Task.Delay(System.TimeSpan.FromSeconds(5));
App.Current.MainWindow.Close();
}
public static async System.Threading.Tasks.Task TestAsync()
{
//nuget Microsoft.Windows.SDK.Contracts
var selectorString = global::Windows.Devices.SmartCards.SmartCardReader.GetDeviceSelector();
var devs = await global::Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(selectorString);
if (devs.Count == 0)
{
return;
}
var dev = devs[0];
var reader = await global::Windows.Devices.SmartCards.SmartCardReader.FromIdAsync(dev.Id);
System.Diagnostics.Debug.WriteLine(reader.Name);
}
}
}