Windows】【DevOps】Windows Server 2022 采用WinSW 启动一个会创建新的控制台程序窗口的程序行为分析

WinSW使用参考

【Windows】【DevOps】Windows Server 2022 采用WinSW将一个控制台应用程序作为服务启动(方便)-CSDN博客

源码

调整ConsoleApp1程序源代码如下:

复制代码
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        // 检查是否作为子进程启动
        if (args.Length > 0 && args[0] == "child")
        {
            ChildProcessMain(args);
        }
        else
        {
            ParentProcessMain();
        }
    }

    static void ParentProcessMain()
    {
        // 获取当前可执行文件的路径
        string currentExePath = System.Reflection.Assembly.GetEntryAssembly()?.Location ?? throw new InvalidOperationException("Could not retrieve entry assembly location.");

        // 设置启动信息
        ProcessStartInfo startInfo = new ProcessStartInfo
        {
            FileName = currentExePath,
            Arguments = "child", // 传递参数 "child"
            UseShellExecute = true, // 使用操作系统外壳程序
            RedirectStandardOutput = false, // 不重定向标准输出
            CreateNoWindow = false // 允许创建新窗口
        };

        // 启动新进程
        using (Process process = Process.Start(startInfo))
        {
            if (process != null)
            {
                Console.WriteLine("Child process started successfully.");
            }
            else
            {
                Console.WriteLine("Failed to start the child process.");
            }
        }

        Console.WriteLine("Parent process continues to run...");
        Console.ReadLine(); // 防止主进程立即退出
    }

    // 子进程部分 - 需要添加到你的可执行文件中
    static void ChildProcessMain(string[] args)
    {
        if (args.Length > 0 && args[0] == "child")
        {
            Console.WriteLine($"Child process received argument: {args[0]}");

            // 获取程序的启动目录
            string startupDirectory = Directory.GetCurrentDirectory();

            // 文件路径
            string filePath = Path.Combine(startupDirectory, "log.txt");

            // 使用 StreamWriter 写入文件
            using (StreamWriter writer = File.AppendText(filePath))
            {
                while (true)
                {
                    writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    writer.Flush();
                    Thread.Sleep(1000); // 每秒写入一次
                }
            }
        }
        else
        {
            Console.WriteLine("Child process did not receive expected argument.");
        }
    }
}

桌面启动时效果

主程序启动后,弹出了子程序控制台窗口

WinSW服务启动效果

日志

out.log中只有主程序的日志,子程序的标准输出日志丢失了。

可以调整子程序标准输出重定向到主程序来解决。

ProcessStartInfo startInfo = new ProcessStartInfo

{

FileName = currentExePath,

Arguments = "child", // 传递参数 "child"

UseShellExecute = true, // 使用操作系统外壳程序

RedirectStandardOutput = true, // 重定向标准输出

CreateNoWindow = false // 允许创建新窗口

};

子进程状态

观察子进程日志文件,可以确定子进程除了不显示窗口,其他部分工作正常。

关于关闭服务

复制代码
Stop-Service ConsoleApp1Service

指令成功调用后,ConsoleApp1.exe自己启动起来的子进程不会被杀死。

相关推荐
YCOSA20257 小时前
Windows11_InsiderPreview_EnterpriseVL_x64_zh-cn_29680_1000.iso 官方直链
windows
怎么没有名字注册了啊9 小时前
Windows Server 2008 R2 如何开启Aero
windows
lingchen190610 小时前
Windows CMD 的 `cd`(change directory,切换目录)命令
windows·命令模式
ITHAOGE1512 小时前
下载 | Win11 26H2 正式版发布!(ISO映像、2026年度版本、26300.9457、Windows 11)
windows·科技·微软·电脑
YCOSA202513 小时前
雨晨 Windows 11 IoT 企业版 Edge 轻装 VCDX 29680.1000
windows·物联网
笨笨努力中14 小时前
2002年Windows XP系统下载视频资源迁移云盘
windows
l1t15 小时前
修复WSL CreateInstance/E_UNEXPECTED和 mounted read-only 错误
linux·windows·wsl
kakakahahahaha16 小时前
Windows电脑卡死恢复流程:任务管理器、Ctrl Alt Delete、explorer与DWM重启
windows·电脑·笔记本电脑·软件需求·电脑卡死
kakakahahahaha16 小时前
Windows鼠标失灵排查:Ctrl Alt Delete、Mouse Keys、USB控制器与驱动修复
windows·驱动开发·电脑·笔记本电脑·软件需求
Vex2une17 小时前
Windows 资源监视器发展史:从任务管理器附件到系统级诊断利器
windows·系统监控·任务管理器·诊断工具·etw·resmon·资源监视器