繁体   English   中英

使用参数启动ac#程序的第二个实例,程序的第一个实例使用该参数

[英]Start 2nd instance of a c# program with a parameter, which the 1st instance of the program uses

我有一个单实例程序,当它运行时,我可以选择将一个参数传递给它。

如果程序没有参数运行,则只需在选项卡上打开dataGridView并将客户列表加载到其中即可。 如果我双击一行,它将在第二个选项卡中打开该特定客户,并提供更多信息。

如果我使用参数(客户ID号从00000到99999)启动程序,它将自动切换到第二个选项卡并加载各个客户数据。

到目前为止,到目前为止,我想做的就是让我的程序运行,但是如果使用参数(例如Program.exe 1234)调用该程序的第二个实例,我希望它直接跳转到第二个标签并显示该客户的详细信息。

到目前为止,这就是我所拥有的。 我是用尝试的方式吠叫错误的树吗? 我得到的印象是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; }

}

啊哈。。。找到答案了……一个叫MadHatter的人制作了一个SingleInstance应用,该应用完全符合我的期望,而且看起来很简单。

http://sanity-free.org/misc/SingleInstanceApp.zip

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM