Windows服务(错误1053)事件日志触发器
本文关键字:事件 日志 触发器 1053 服务 错误 Windows | 更新日期: 2023-09-27 17:57:57
我是编程新手,我将在下周二完成这个项目。我想做的是,如果用户在登录屏幕上输入了错误的密码,摄像头就会拍照。我试图在服务中实现我的代码,但它给了我错误1053。我想知道是否有人可以为我修复这个代码,或者文件观察程序是否在我的代码中有用。请帮忙!
namespace SampleWS
{
public partial class Service1 : ServiceBase
{
private WebCam camera;
public Service1()
{
InitializeComponent();
}
public void OnDebug()
{
OnStart(null);
}
protected virtual void OnPause(string[] args)
{
bool infinite = false;
LogonChecker(infinite);
}
protected virtual void OnContinue(string[] args)
{
bool infinite = true;
LogonChecker(infinite);
}
protected override void OnStart(string[] args)
{
bool infinite = true;
LogonChecker(infinite);
}
protected override void OnStop()
{
bool infinite = false;
LogonChecker(infinite);
}
DateTime mytime = DateTime.MinValue;
public void LogonChecker(bool infinity)
{
string queryString =
"<QueryList>" +
" <Query Id='"'" Path='"Security'">" +
" <Select Path='"Security'">" +
" *[System[(Level <= 0) and" +
" TimeCreated[timediff(@SystemTime) <= 86400000]]]" +
" </Select>" +
" <Suppress Path='"Application'">" +
" *[System[(Level = 0)]]" +
" </Suppress>" +
" <Select Path='"System'">" +
" *[System[(Level=1 or Level=2 or Level=3) and" +
" TimeCreated[timediff(@SystemTime) <= 86400000]]]" +
" </Select>" +
" </Query>" +
"</QueryList>";
camera = new WebCam();
while (infinity)
{
EventLogQuery eventsQuery = new EventLogQuery("Security", PathType.LogName, queryString);
eventsQuery.ReverseDirection = true;
EventLogReader logReader = new EventLogReader(eventsQuery);
EventRecord eventInstance;
Int32 eventexists3 = new Int32();
EventLog mylog = new EventLog();
for (eventInstance = logReader.ReadEvent(); null != eventInstance; eventInstance = logReader.ReadEvent())
{
eventexists3 = eventInstance.Id.CompareTo(4625);
if (eventexists3 == 0)
{
if (eventInstance.TimeCreated.Value > mytime)
{
mytime = eventInstance.TimeCreated.Value;
camera.Connect();
Image image = camera.GetBitmap();
image.Save(@"D:'Audio'testimage3.jpg");
camera.Disconnect();
eventInstance = null;
break;
}
}
EventLogRecord logRecord = (EventLogRecord)eventInstance;
LogonChecker(infinity);
}
}
}
}
}
尽管有我的评论,但这很容易。检查错误1053的含义:
ERROR_SERVICE_REQUEST_TIMEOUT
1053(0x41D)
该服务没有及时响应启动或控制请求。
您对ServiceBase
方法(如OnStart
)的重写需要尽快返回。如果您想执行任何正在进行的工作,请订阅事件或启动工作线程。
MSDN上的.NET文档并没有真正涵盖太多关于服务执行模型的内容,为此,您需要查看Win32文档:关于服务。