Pregunta

I am new to programming and I have this project due on coming Tuesday. What I am trying to do is if a user enters a wrong password on the logon screen the cam takes the picture. I tried to implement my code in services but it gives me error 1053. I was wondering if somebody could fix this code for me or if file watcher is of use in my code. Please help!

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 &lt;= 0) and" +
               "        TimeCreated[timediff(@SystemTime) &lt;= 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) &lt;= 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);
               }
            }
        }
    }
}
¿Fue útil?

Solución

Despite my comment, this is easy. Check what the error 1053 means:

ERROR_SERVICE_REQUEST_TIMEOUT

1053 (0x41D)

The service did not respond to the start or control request in a timely fashion.

Your overrides of ServiceBase methods like OnStart need to return as soon as possible. If you want to perform any ongoing work either subscribe to events or spin up a worker thread.

The .NET documentation on MSDN doesn't really cover much about the execution model of services, for this you need to look at the Win32 documentation: About Services.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top