c# - 控制台应用程序的 Process.Start() 在 Server 2008 R2 中无法从 Windows 服务运行

标签 c# windows-services console-application windows-server-2008-r2 process.start

更新(美国东部时间 2014 年 3 月 20 日下午 5:11):已添加代码以编写“GotHere0.txt”。 Gothere0、1、2、3 看起来都很好。 Gothere4 没有出现。

请注意 - 这不是重复的 - 它与我发现的所有现有线程有非常细微的差异。

要求:

  1. 在 Windows Server 2008 R2 中从本地系统帐户下运行的 Windows 服务启动控制台应用程序
  2. 控制台应用程序内部的代码写入 SQL 数据库,并将文件写入驱动器,但不需要用户输入(也不需要查看控制台本身或与之交互)。我们只需要来自控制台应用程序的代码即可执行。
  3. 这应该在服务器启动时工作,不需要用户登录。

目前的结果:

Process.Start(sInfo) 完成,就好像一切都成功了一样。没有异常被抛出。返回进程 ID > 0。但是,很明显,testapp.exe 中没有任何代码实际执行。有谁知道我该如何解决这个问题?谢谢!

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading.Tasks;

namespace TestService
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            System.IO.File.WriteAllText(@"c:\temp\GotHere0.txt", "");
            System.Threading.Tasks.Task.Factory.StartNew(() => Run(), TaskCreationOptions.LongRunning);
        }
        protected override void OnStop()
        {
            ContinueRunning = false;
        }

        public static bool ContinueRunning = true;
        public void Run()
        {
            System.IO.File.WriteAllText(@"c:\temp\GotHere1.txt", "");
            while (ContinueRunning)
            {
                try
                {
                    List<string> args = new List<string>();
                    args.Add("Test");
                    StartApp(@"c:\temp\testapp.exe", args);
                }
                catch (Exception)
                { }

                if (ExitEarly()) return;
            }
        }

        private static void StartApp(string exePath, List<string> args)
        {
            try
            {
                System.IO.File.WriteAllText(@"c:\temp\GotHere2.txt", "");
                ProcessStartInfo sInfo = new ProcessStartInfo();
                sInfo.FileName = exePath;
                sInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(exePath);
                sInfo.Arguments = string.Join(" ", args);
                sInfo.CreateNoWindow = false;
                Process runningProcess = Process.Start(sInfo);

                string message = "";
                if (runningProcess!=null)
                {
                    message = runningProcess.Id.ToString();
                }
                System.IO.File.WriteAllText(@"c:\temp\GotHere3.txt", message);
            }
            catch (Exception exc)
            {
                System.IO.File.WriteAllText(@"c:\temp\GotHere4.txt", exc.Message);
            }
        }
        private static bool ExitEarly()
        {
            // Sleep for a total of 60 seconds, and return if "OnStop" is called.
            for (int i = 0; i < 600; i++)
            {
                System.Threading.Thread.Sleep(100);
                if (!ContinueRunning) return true;
            }
            return false;
        }
    }
}

这是 testapp.exe 的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace testapp
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();
            if (args == null || args.Length == 0)
            {
                sb.AppendLine("NO arguments were passed.");
            }
            else
            {
                foreach (string arg in args)
                {
                    sb.AppendLine("Arg: [" + arg + "]");
                }
            }
            System.IO.File.WriteAllText("helloworld.txt", sb.ToString());
        }
    }
}

enter image description here

最佳答案

如果您希望从正在启动的应用程序中看到 UI,那您不会。您无法从服务启动 UI。

关于c# - 控制台应用程序的 Process.Start() 在 Server 2008 R2 中无法从 Windows 服务运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22543441/

相关文章:

c# - StreamReader.Read 返回\0

java - 将java应用程序作为windows服务

delphi - 防止在 Delphi 控制台应用程序中闪烁

C# Linq to SQL 无效转换异常

c# - 找不到类型或命名空间名称 'EventHandler'

c# - 当 MSMQ 服务器重新启动时,远程 MSMQ 连接显然丢失问题

java - 在 Java 控制台应用程序中执行 KeyListener

c# - 制作一个由计划任务启动且不得显示任何窗口的程序

C# 复制 powerpoint 主幻灯片布局

python - 长时间运行进程的超时和 Windows 服务 (Python)