从 C# 启动远程进程的问题

发布于 2024-11-17 06:09:27 字数 545 浏览 0 评论 0原文

我正在使用 System.Diagnostics.Process.Start 启动远程另一台域计算机上的应用程序。

不幸的是,如果远程进程已经在运行,但在远程计算机桌面的后台,则应用程序无法使用 Process.Start 获得焦点。

问题1:是否有其他API或机制可以强制远程应用程序获得焦点或闪烁以引起用户的注意?

我注意到的另一个问题是,如果远程进程已经在运行,除了原始实例之外,还可能会执行一个新实例。这违反了 MSDN 的文档:

“如果进程已经在运行,则不会启动额外的进程资源。相反,会重用现有的进程资源,并且不会创建新的进程组件。在这种情况下,Start 返回 null,而不是返回新的进程组件到调用过程。”

问题 2: 有没有人找到一种方法来防止在这种情况下启动应用程序的第二个实例? WMI 是远程启动应用程序的更好选择吗?

I'm using System.Diagnostics.Process.Start to launch a remote application on another domain machine.

Unfortunately, if the remote process is already running, but in the background of the remote machine's desktop, the application does not gain focus using Process.Start.

Question 1: Is there another API or mechanism to force the remote application to gain focus, or flash to get the user's attention?

The other issue I noticed, is if the remote process is already running, a new instance may be executed in addition to the original. This violates MSDN's documentation which says:

"If the process is already running, no additional process resource is started. Instead, the existing process resource is reused and no new Process component is created. In such a case, instead of returning a new Process component, Start returns null to the calling procedure."

Question 2: Has anyone found a way to prevent a second instance of the application from launching in this case? Is WMI a better choice to use for remote launching of applications?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扮仙女 2024-11-24 06:09:27

好吧,不知道这对您来说效果如何,但它是您可以在帮助程序中使用的示例类。这只是开始,如果您打算使用它,您将需要一个网络系统(尽管使用 C# 也不错)。告诉我它对你有什么作用。

/// <summary>
/// Allows you to start a specified program, or if it is already running, bring it into focus
/// </summary>
static class SFProgram
{
    static public void StartFocus(string FileName, string ProcessName)
    {
        if (!ProcessStarted(ProcessName))
            Process.Start(FileName);
        else
            SFProgram.BringWindowToTop("notepad");
    }

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    /// <summary>
    /// Bring specified process to focus
    /// </summary>
    /// <param name="windowName">Process Name</param>
    /// <returns>If it was successful</returns>
    private static bool BringWindowToTop(string windowName)
    {
        Process[] processes = Process.GetProcessesByName(windowName);
        foreach (Process p in processes)
        {
            int hWnd = (int)p.MainWindowHandle;
            if (hWnd != 0)
            {
                return SetForegroundWindow((IntPtr)hWnd);
            }
            //p.CloseMainWindow();
        }
        return false;
    }

    private static bool ProcessStarted(string ProcessName)
    {
        Process[] processes = Process.GetProcessesByName(ProcessName);
        return (processes.Length > 0);
    }
}

Well, don't know how well this will work for you, but it is an example class that you could use in the helper program. This is only the start, if you plan on using it, you will need a networking system (not to bad with C# though). Tell me how it works for you.

/// <summary>
/// Allows you to start a specified program, or if it is already running, bring it into focus
/// </summary>
static class SFProgram
{
    static public void StartFocus(string FileName, string ProcessName)
    {
        if (!ProcessStarted(ProcessName))
            Process.Start(FileName);
        else
            SFProgram.BringWindowToTop("notepad");
    }

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    /// <summary>
    /// Bring specified process to focus
    /// </summary>
    /// <param name="windowName">Process Name</param>
    /// <returns>If it was successful</returns>
    private static bool BringWindowToTop(string windowName)
    {
        Process[] processes = Process.GetProcessesByName(windowName);
        foreach (Process p in processes)
        {
            int hWnd = (int)p.MainWindowHandle;
            if (hWnd != 0)
            {
                return SetForegroundWindow((IntPtr)hWnd);
            }
            //p.CloseMainWindow();
        }
        return false;
    }

    private static bool ProcessStarted(string ProcessName)
    {
        Process[] processes = Process.GetProcessesByName(ProcessName);
        return (processes.Length > 0);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文