using System;
using System.Windows;
using IpcUtility;
namespace IpcArgsTest
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
private const string AppGuid = "{a1b2c3d4-e5f6-a1b2-c3d4-e5f6a1b2c3d4}";
private IpcManager ipcMgr;
public MainWindow()
{
InitializeComponent();
textBox.Text = "";
}
//情報表示
private void Info(string mes)
{
textBox.Text += mes + Environment.NewLine;
}
//
private void Info(string[] mess)
{
foreach (string mes in mess) { Info(mes); }
}
/// <summary>
/// FrameworkElement(この場合MainWindow)の初期化が完了した状態。
/// Windowに配置した要素の準備はできていない。
/// </summary>
private void Window_Initialized(object sender, EventArgs e)
{
//IPC確立・兼・二重起動確認
ipcMgr = new IpcManager(AppGuid);
ipcMgr.Connect(this, IpcCallback_ClientStarted);
if (ipcMgr.IsServer)
{
this.Title = "サーバー";
}
else
{
this.Title = "クライアント"; //「this.Title = "サーバー"」に対する便宜的な処理
//MainWindowは表示前にクローズするので実際に目にすることはない
this.Close(); //二重起動禁止の場合はここで自身を終了させる
}
}
/// <summary>
/// FrameworkElement(この場合MainWindow)と、そこに含まれる要素の配置と描画が完了した状態。
/// </summary>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//コマンドライン引数を利用する何らかの処理へ
Info("アプリ起動時のコマンドライン引数は");
DisplayArgs(Environment.GetCommandLineArgs());
}
//アプリ終了
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
ipcMgr?.Dispose();
}
//2個目の本アプリ起動により呼び出される
private void IpcCallback_ClientStarted()
{
//コマンドライン引数を利用する何らかの処理へ
Info("クライアントから送られてきたコマンドライン引数は");
var args = ipcMgr.ReceiveArgs();
DisplayArgs(args);
}
//コマンドライン引数を利用する何らかの処理
//・サーバー起動時とクライアント起動時の、コマンドライン引数処理の合流点。
private void DisplayArgs(string[] args)
{
Info(args);
}
}
}