C# 控制台应用程序将进程带到前台

标签 c# c#-4.0 console-application

使用以下 C# 控制台应用程序代码,我可以使用 Jenkins 在后台运行该进程。但现在我想在前台看到这个过程。我在这里做错了什么?

[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr handle);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool IsIconic(IntPtr handle);

private void startT32app()
{
    IntPtr handle;
    try
    {
        Console.WriteLine("T32 launching");
        string path = @"C:\T32\bin\windows64\t32mppc.exe";
        string args = @"C:\T32\config.t32";
        ProcessStartInfo procInfo = new ProcessStartInfo(path, args);
        procInfo.CreateNoWindow = false;
        procInfo.UseShellExecute = true;
        procInfo.WindowStyle = ProcessWindowStyle.Normal;

        Process procRun = Process.Start(procInfo);
        handle = procRun.MainWindowHandle;
        SetForegroundWindow(handle);
    }
    catch
    {
        Console.WriteLine("Failed to launch T32");
    }
}
static void Main(string[] args)
{ 
    Program Beginapps = new Program();
    Beginapps.startT32app();        
}

最佳答案

完成您的任务的一个选项是将 shift + tab 发送到窗口以将其设置在所有内容的前面(我在另一个应用程序中尝试了不同的方式,但只有这个对我有用):

// is used to set window in front
[DllImport("User32.dll", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

public void startT32app()
{
    IntPtr handle;
    try
    {
        Console.WriteLine("T32 launching");
        string path = @"C:\T32\bin\windows64\t32mppc.exe";
        string args = @"C:\T32\config.t32";
        ProcessStartInfo procInfo = new ProcessStartInfo(path, args);
        procInfo.CreateNoWindow = false;
        procInfo.UseShellExecute = true;
        procInfo.WindowStyle = ProcessWindowStyle.Normal;

        Process procRun = Process.Start(procInfo);
        handle = procRun.MainWindowHandle;
        SwitchToThisWindow(handle, true);
    }
    catch
    {
        Console.WriteLine("Failed to launch T32");
    }
}

关于C# 控制台应用程序将进程带到前台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46974192/

相关文章:

multithreading - 引发事件,以便在与原始订阅者相同的线程上处理它们

c# - 为什么当文本突出显示时 C# 控制台应用程序停止运行?

Clojure(读行)不等待输入

c# - 页面退出时执行操作

c# - 为什么 Context.RewritePath 维护查询字符串?

c# - 当一切看起来都很好时,有条件的不起作用

javascript - 使用 JavaScript 在 mvc 4 中使用 C# 在部分 View 中使用 HTML 打印 div

asp.net - ssl 在第一次登录时让我的 session 为空

c# - 我怎么写 Console.Writeline ("\|\/\|/");并忽略 "Unrecognized escape sequence"错误?

c# - NHibernate 将类属性映射到行而不是列