Skip to content

Commit

Permalink
NiconamaCommentViewerがすべて終了したとき、本アプリを自動的に終了するように
Browse files Browse the repository at this point in the history
esperecyan committed May 4, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 1011f0b commit c02da1d
Showing 5 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
@@ -36,6 +36,9 @@
<setting name="EnginePort" serializeAs="String">
<value>50021</value>
</setting>
<setting name="ParentProcessName" serializeAs="String">
<value>NiconamaCommentViewer</value>
</setting>
</Esperecyan.NCVVoicevox.Properties.Settings>
</applicationSettings>
</configuration>
15 changes: 15 additions & 0 deletions App.xaml.cs
Original file line number Diff line number Diff line change
@@ -119,6 +119,14 @@ private App()
Settings.Default.Save();
}

var ncvWatcher = new ProcessWatcher(Settings.Default.ParentProcessName);
if (ncvWatcher.IsAllExited)
{
MessageBox.Show($"「{Settings.Default.ParentProcessName}」が起動していません。", App.Title);
this.Shutdown();
return;
}

var waveOut = new WaveOut();

var engineServer = new EngineServer(Settings.Default.EnginePort);
@@ -194,5 +202,12 @@ private App()
engineServer.Dispose();
notifyIcon.Dispose();
};

ncvWatcher.AllExited += (_, _) =>
{
engineServer.Dispose();
notifyIcon.Dispose();
Environment.Exit(0);
};
}
}
36 changes: 36 additions & 0 deletions ProcessWatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Diagnostics;

namespace Esperecyan.NCVVoicevox;

/// <summary>
/// 特定の名前のプロセスがすべて終了することを監視します。
/// </summary>
internal class ProcessWatcher
{
internal event EventHandler? AllExited;
internal bool IsAllExited;

private readonly string name;

internal ProcessWatcher(string name)
{
this.name = name;
this.Watch();
}

private void Watch()
{
var processes = Process.GetProcessesByName(this.name);
if (processes.Length == 0)
{
this.IsAllExited = true;
this.AllExited?.Invoke(this, EventArgs.Empty);
Debug.WriteLine(1);
return;
}

processes[0].EnableRaisingEvents = true;
processes[0].Exited += (_, _) => this.Watch();
}
}
9 changes: 9 additions & 0 deletions Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Properties/Settings.settings
Original file line number Diff line number Diff line change
@@ -14,6 +14,9 @@
<Setting Name="EnginePort" Type="System.Int32" Scope="Application">
<Value Profile="(Default)">50021</Value>
</Setting>
<Setting Name="ParentProcessName" Type="System.String" Scope="Application">
<Value Profile="(Default)">NiconamaCommentViewer</Value>
</Setting>
<Setting Name="VoicevoxPath" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>

0 comments on commit c02da1d

Please sign in to comment.