Windows桌面服务在安装时不起作用,但在打印时工作正常 [英] Windows Desktop Service is Not Working when Installed But Works Fine While Debuging
本文介绍了Windows桌面服务在安装时不起作用,但在打印时工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Management;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace WindowsService1
{
public partial class ShutDownServices : ServiceBase
{
FileStream fs, log;
string path;
StreamWriter sw;
static int Count = 0;
public ShutDownServices()
{
InitializeComponent();
try
{
path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).ToString();
fs = new FileStream(path + "\\" + "ShutDownDetails.txt",FileMode.OpenOrCreate);
log = new FileStream(path + "\\" + "ErrorLogin.txt",FileMode.OpenOrCreate);
}
catch (Exception ex)
{
}
finally
{
fs.Close();
log.Close();
}
}
protected override void OnStart(string[] args)
{
// EventLog.WriteEntry("Starting MyService"+DateTime.Now.ToString());
timer1.Enabled = true;
timer1.Start();
}
[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int operationFlag, int operationReason);
protected override void OnStop()
{
// EventLog.WriteEntry("Stopping MyService"+DateTime.Now.ToString());
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
if (Count == 0)
if (DateTime.Now.Hour >=10 && DateTime.Now.Minute>= 0)
{
sw = new StreamWriter(path + "\\" + "ShutDownDetails.txt", true);
sw.WriteLine(DateTime.Now.ToShortDateString());
sw.WriteLine(DateTime.Now.ToShortTimeString());
sw.WriteLine("----------------------ShutDown This Machine At this Time-----------------");
sw.Flush();
sw.Close();
System.Diagnostics.Process.Start("shutdown", "/s /t");
//ExitWindowsEx(0, 0);
Count++;
timer1.Stop();
}
}
catch (Exception ex)
{
sw = new StreamWriter(path + "\\" + "ErrorLogin.txt", true);
sw.WriteLine(DateTime.Now.ToShortDateString());
sw.WriteLine(DateTime.Now.ToShortTimeString());
sw.WriteLine("----------------------Error Occurs At this Time-----------------");
sw.Flush();
sw.Close();
}
}
}
}
1)我的完整代码在这里,这是关闭或在给定时间注销电脑
1)my Complete Code is here ,this for shutdown or log off pc at given time
推荐答案
你看这个吗?
http://stackoverflow.com/questions/158371/error-1053-the-service-did-not-respond-to-the-start-or-control-request-in -a-tim [ ^ ]
这篇关于Windows桌面服务在安装时不起作用,但在打印时工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文