私は単一のインスタンス プログラムを持っており、実行時に単一のパラメーターを渡すことを選択できます。
プログラムがパラメーターなしで実行される場合は、タブで dataGridView を開き、顧客のリストをそこにロードします。行をダブルクリックすると、その特定の顧客が 2 番目のタブに開き、詳細情報が表示されます。
パラメータ (00000 から 99999 までの顧客 ID 番号) を指定してプログラムを開始すると、自動的に 2 番目のタブに切り替わり、個々の顧客データが読み込まれます。
これまでのところ、しかし、私ができるようにしたいのは、プログラムを実行することですが、プログラムの2番目のインスタンスがパラメーターで呼び出された場合、たとえばProgram.exe 1234に直接ジャンプしたい2 番目のタブで、その顧客の詳細を表示します。
これは私がこれまでに持っているものです。私がこれをやろうとしている方法で間違ったツリーを吠えていますか? Program.exe は、実行中の別のインスタンスをリッスンし、渡されたパラメーターを使用する必要があるという印象を受けました。
アドバイスをいただければ幸いです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Reflection;
using System.IO;
namespace SiteConnex
{
public class SingleApplication
{
/// Imports
[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int IsIconic(IntPtr hWnd);
public static bool secondInstance = false;
public static string siteid = "";
public static bool gotosite = false;
public static void Main(string[] args)
{
// string siteid = "";
// bool gotosite = false;
// bool secondInstance = false;
int testSiteid = 0;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
siteid = Convert.ToString(args[0]);
}
catch (Exception noArgs)
{
// No commandline args have been passed.
}
// check that the parameter passed is actually a valid number, otherwise just act like no params passed.
try
{
testSiteid = Convert.ToInt32(siteid);
if ((testSiteid > 00000) || (testSiteid < 99999))
{
gotosite = true;
}
else
{
siteid = "";
}
}
catch (Exception e)
{
// If you get an exception it means siteid had duff data passed into it.
}
/// Check if it's running, if it is, pass siteid and gotosite through... and acts as if it has a valid siteid passed to it.
// Application.Run(new Form1(siteid, gotosite));
Run(siteid, gotosite, secondInstance);
}
private static IntPtr GetCurrentInstanceWindowHandle()
{
IntPtr hWnd = IntPtr.Zero;
Process process = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(process.ProcessName);
foreach(Process _process in processes)
{
// Get the first instance that is not this instance, has the
// same process name and was started from the same file name
// and location. Also check that the process has a valid
// window handle in this session to filter out other user's
// processes.
if (_process.Id != process.Id &&
_process.MainModule.FileName == process.MainModule.FileName &&
_process.MainWindowHandle != IntPtr.Zero)
{
hWnd = _process.MainWindowHandle;
break;
}
}
return hWnd;
}
/// SwitchToCurrentInstance
private static void SwitchToCurrentInstance(string siteid, bool gotosite, bool secondInstance)
{
IntPtr hWnd = GetCurrentInstanceWindowHandle();
if (hWnd != IntPtr.Zero)
{
// Restore window if minimised. Do not restore if already in
// normal or maximised window state, since we don't want to
// change the current state of the window.
if (IsIconic(hWnd) != 0)
{
ShowWindow(hWnd, SW_RESTORE);
}
// Set foreground window.
SetForegroundWindow(hWnd);
secondInstance = true;
}
}
/// Execute a form base application if another instance already running on
/// the system activate previous one
/// <param name="frmMain">main form</param>
/// true if no previous instance is running
// public static bool Run(System.Windows.Forms.Form frmMain)
public static bool Run(string siteid, bool gotosite, bool secondInstance)
{
if(IsAlreadyRunning())
{
//set focus on previously running app
SwitchToCurrentInstance(siteid, gotosite, secondInstance);
return false;
}
Application.Run(new Form1(siteid, gotosite, secondInstance));
return true;
}
/// check if given exe alread running or not
/// returns true if already running
private static bool IsAlreadyRunning()
{
string strLoc = Assembly.GetExecutingAssembly().Location;
FileSystemInfo fileInfo = new FileInfo(strLoc);
string sExeName = fileInfo.Name;
bool bCreatedNew;
mutex = new Mutex(true, "Global\\"+sExeName, out bCreatedNew);
if (bCreatedNew)
mutex.ReleaseMutex();
return !bCreatedNew;
}
static Mutex mutex;
const int SW_RESTORE = 9;
}
}