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

相关推荐
路由侠内网穿透2 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
研华嵌入式2 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
带娃的IT创业者3 天前
Windows 平台上基于 MCP 构建“文心一言+彩云天气”服务实战
人工智能·windows·文心一言·mcp
csdn_aspnet3 天前
Windows Node.js 安装及环境配置详细教程
windows·node.js
摇滚侠3 天前
java语言中,list<String>转成字符串,逗号分割;List<Integer>转字符串,逗号分割
java·windows·list
Source.Liu3 天前
【Pywinauto库】12.2 pywinauto.element_info 后端内部实施模块
windows·python·自动化
Source.Liu3 天前
【Pywinauto库】12.1 pywinauto.backend 后端内部实施模块
开发语言·windows·python·自动化
私人珍藏库3 天前
[Windows] FileOptimizer v17.1.0_一款文件批量压缩工具
windows·批量压缩
掘根3 天前
【CMake】List
windows·microsoft·list
TToolss3 天前
删除文件夹里的网盘图标
windows