【问题标题】:Windows service that runs a batch file运行批处理文件的 Windows 服务
【发布时间】:2012-01-18 09:56:01
【问题描述】:

我开发了一个在 Windows 服务器上运行的 Windows 服务。该服务的目的是在本地系统上运行批处理文件,该批处理文件进一步运行基于 Java 的线程。问题是,当我使用远程会话登录服务器时,服务正常启动,但批处理文件和 Java 线程都在后台运行,但是当我在不使用远程会话的情况下登录服务器时,即物理上去服务器所在的位置,那么两者出现 Java 线程和批处理文件窗口。我的问题是,当我使用远程会话登录服务器时,如何防止批处理文件和 Java 线程在后台运行。运行批处理文件的代码附在下面:

public void RunBatchFile()
        {
            while (!this.isStopped)
            {
                while (StartnStop)
                {
                    foreach (object element in apps)
                    {
                        App_arr chkapp = (App_arr)element;

                        System.DateTime now_date = System.DateTime.Now;
                        System.DateTime last_date = new System.DateTime(chkapp.last_time.Year, chkapp.last_time.Month, chkapp.last_time.Day, chkapp.last_time.Hour, chkapp.last_time.Minute, chkapp.last_time.Second);

                        System.TimeSpan time_span = now_date.Subtract(last_date);


                        if (time_span.Minutes >= chkapp.mins)
                        {
                          try
                            {
                                p = new Process();

                                string targetDir = string.Format(@chkapp.app_path.ToString().Substring(0, chkapp.app_path.ToString().LastIndexOf("\\")));
                                p.StartInfo.WorkingDirectory = targetDir;
                                string batch_file_name = chkapp.app_path.ToString().Substring(chkapp.app_path.ToString().LastIndexOf("\\") + 1);
                                p.StartInfo.FileName = batch_file_name;

                                p.StartInfo.Arguments = string.Format("C-Sharp CTF-Service Application");
                                p.StartInfo.CreateNoWindow = false;
                                //p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                                p.Start();


                            }
                            catch (Win32Exception ex1)
                            {                              
                                log.WriteEntry(ex1.Message + "\n" + ex1.StackTrace, EventLogEntryType.Error);
                                sw.BaseStream.Seek(0, SeekOrigin.End);
                                sw.WriteLine(ex1.Message);
                                sw.Flush();
                            }
                        }
                    }
                    Thread.Sleep(40000);
                }
            }
            fs.Close();

        }

【问题讨论】:

  • 您是否尝试将p.StartInfo.CreateNoWindow = false; 设置为True

标签: c# windows-services


【解决方案1】:

在你的代码中

 p.StartInfo.Arguments = string.Format("C-Sharp CTF-Service Application");
 p.StartInfo.CreateNoWindow = true; //Instead of false
 //Try this if above line doesn't work
 p.StartInfo.UseShellExecute = false;

注意:

如果UserNamePassword 属性不是Nothing,则CreateNoWindow 属性值将被忽略并创建一个新窗口。 (MSDN)

希望这行得通。

【讨论】:

    猜你喜欢
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    相关资源
    最近更新 更多