双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

service监视启动控制台应用程序问题

jkffkle jkffkle 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我写了个服务Monitor,用来监视本地一个控制台应用程序Task.exe,如果Task意外停止,Monitor负责重启Task,写好以后,Monitor可以重启Task,但是Task的界面不会弹出来,只能在任务管理器里看到Task的进程,通过Task的运行日志,也可以看到Task的各线程运行正常,以下为我服务里的主要代码:

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

protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
WriteLog("启动服务", "Monitor");

}

protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
WriteLog("服务停止", "Monitor");
}


public int RunTask()
{
//MessageBox.Show(string.Format("重启Monitor_Task服务"));
WriteLog("开始重启Task程序","Monitor");

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "Task1.exe";
info.WorkingDirectory = @"C:\Users\Administrator\Documents\Visual Studio 2005\Projects\Task1\Task1\bin\Debug\";
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
Process proc_satrt = new Process();
proc_satrt.StartInfo = info;
try
{
proc_satrt.Start();
Thread.Sleep(500);
}
catch (Exception ex)
{
WriteLog(string.Format("重启Task出现错误,错误原因:{0}__{1}",ex.Message,ex.StackTrace),"Monitor");
}

Process[] proc_task = Process.GetProcessesByName("Task1");
if (proc_task != null || proc_task.Length > 1)
{
WriteLog("重启Task程序成功", "Monitor");
return proc_task.Length;
}
else
{
WriteLog("重启Task程序失败", "Monitor");
}
return 0;
}

private void timer2_ElapsedEventHandler(object sender, ElapsedEventArgs e)
{
Monitor();
}

public void Monitor()
{
while (true)
{
//Process[] collection = Process.GetProcesses();
//foreach (Process proc in collection)
//{
// WriteLog(proc.Id.ToString()+"__"+proc.ProcessName+"__"+proc.Handle.ToString()+"__","Monitor");
//}

//WriteLog("执行timer1","Monitor");
string Task_pro_name = "Task1";
Process[] pros = Process.GetProcessesByName(Task_pro_name);
if (pros != null && pros.Length > 0)
{
foreach (Process pro in pros)
{
//MessageBox.Show(string.Format("获取线程:{0}",pro.Id.ToString()));
WriteLog(string.Format("获取线程:{0}___{1}", pro.Id.ToString(), pro.ProcessName),"Monitor");
int int_th = pro.Threads.Count;
int int_thcount = pro.Threads.Count;
//获取进程的线程组
System.Diagnostics.ProcessThreadCollection threads = pro.Threads;
foreach (ProcessThread th in threads)
{
if (th.ThreadState == System.Diagnostics.ThreadState.Unknown)
{
--int_th;
}

if (int_th < int_thcount) //发现有线程死掉,结束所有线程
{
pro.Kill();
pro.CloseMainWindow();
}
}
}
}
else
{
//启动Task程序
RunTask();
}
Thread.Sleep(30000);
}
}


public void WriteLog(string contex,string type)
{
string contring = "Server=LocalHost;Integrated Security=SSPI;Database=DMP";
SqlConnection sqlcon = new SqlConnection(contring);
string sqlstring = "insert into MonitorServiceLog(M_content,M_type,M_time) values(@contex,@type,GETDATE())";
SqlCommand sqlcom = new SqlCommand(sqlstring, sqlcon);
try
{
if (sqlcon.State == ConnectionState.Closed)
{
sqlcon.Open();
}
sqlcom.Parameters.Add("@contex", SqlDbType.NVarChar, 500).Value = contex;
sqlcom.Parameters.Add("@type", SqlDbType.NVarChar, 20).Value = type;
sqlcom.ExecuteNonQuery();
}
catch (Exception ex)
{

}
finally
{
sqlcon.Close();
sqlcom.Dispose();
}
}



}



我想知道为什么启动不了Task.exe这个控制台程序的界面?请高手帮忙,先谢谢,在线等回复
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
jkffkle
.NET技术其他语言 1,979 成员 12,447 内容