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自己启动起来的子进程不会被杀死

相关推荐
航Hang*2 小时前
Windows Server 配置与管理——第3章:文件系统管理
运维·服务器·windows·vmware
无限进步_3 小时前
【C++】电话号码的字母组合:从有限处理到通用解法
开发语言·c++·ide·windows·git·github·visual studio
私人珍藏库4 小时前
【Windows】PDF超能助手(1.0.13)
windows·pdf·工具·软件·多功能
仟人斩4 小时前
Windows 下把 VSCode 加入右键菜单(注册表方案)
windows·vscode·上下文菜单
大强同学4 小时前
对比 VS Code:Zed 编辑器编码体验全面解析
人工智能·windows·编辑器·ai编程
加勒比之杰克8 小时前
从阻塞 IO 到 epoll:把 Linux 网络 IO 一次讲透
linux·网络·windows·select·多路转接·epoll·poll
涔溪9 小时前
腾讯 WorkBuddy 超详细卸载清理文档(适用于 Windows 1011 + macOS 全版本,彻底卸载、不留残留)
windows·macos·ai·workbuddy
取个名字太难了a9 小时前
等待块(一)
windows
洒满阳光的庄园9 小时前
Tauri Windows 桌面端:环境与构建流程
windows
love530love9 小时前
从零搭建本地版 Claurst:基于 Rust 重构的 Claude Code 终端编码助手 + LM Studio 模型接入测试
开发语言·人工智能·windows·重构·rust·lm studio·claude code