2012-01-18 69 views
0

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

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(); 

     } 
+2

你有没有试过设置'p.StartInfo.CreateNoWindow = false; '为'真'? – klennepette 2012-01-18 10:04:01

回答

0

在你的代码

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属性不没有,则CreateNoWindow属性值将被忽略并创建一个新窗口。 (MSDN

希望这个工程。

相关问题