0

I have implemented the following code with a Timer.Elapsed event which runs a batch process and takes a screenshot of my desktop. The batch process runs perfectly everywhere else in the code except in the ElapsedHandler. I know that the handler is being called properly because I added some code to print to a file, which works perfectly. The batch process itself however, never gets executed. Am I missing something about the Timer that is causing the problem?

using System;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Timers;
using System.Drawing;
using System.Drawing.Imaging;
using System.ServiceProcess;

namespace ScreenCaptureService
{
    public class ScreenCaptureService : ServiceBase
    {
        private const int durationInMinutes = 1;
        private System.Timers.Timer t;

        protected override void OnStart(string[] args)
        {
            t = new System.Timers.Timer((float)(1000));
            t.Elapsed += new ElapsedEventHandler(ElapsedHandler);
            t.Enabled = true;
        }

        protected void ElapsedHandler(object sender, ElapsedEventArgs e)
        {
            string testpath = @"C:\Dump\new.txt";
            if (!File.Exists(testpath))
            {
                File.CreateText(testpath);
                using (StreamWriter sw = File.AppendText(testpath))
                {
                    sw.WriteLine("Initialized");
                }
            }
            else
            {
                using (StreamWriter sw = File.AppendText(testpath))
                {
                    sw.WriteLine("Hello " + DateTime.Now.ToString());
                }
            }
            Process.Start(@"C:\users\wyoung\screenshot.bat");
        }

        protected override void OnStop()
        {
            t.Enabled = false;
        }
    }
}
user3685285
  • 6,066
  • 13
  • 54
  • 95
  • What happens when the code reaches the Process.Start line when you debug it? – saarrrr Aug 26 '15 at 19:34
  • And what is the verifiable behavior of the batch process? – saarrrr Aug 26 '15 at 19:40
  • Ok, so if I run it as a console application, it actually works perfectly. However, once I put it into a Windows Service, it stops working. What is weird is that the txt file output works perfectly in both tests. – user3685285 Aug 26 '15 at 19:48
  • Sounds like an account or permission issue. Make sure the service account has access to the batch file. – saarrrr Aug 26 '15 at 19:54
  • 2
    Services don't have handles to the UI. – zam664 Aug 26 '15 at 19:55
  • http://stackoverflow.com/questions/4516200/how-can-a-windows-service-start-a-process-when-a-timer-event-is-raised/4516515#4516515 – restrada Aug 26 '15 at 19:58

1 Answers1

2

Windows services run from a separate session with a different desktop so your service will not be able to take an screenshot of your desktop (at least not without a lot of work).

You'll have to either run this as a scheduled task or as a program that runs on startup.

Community
  • 1
  • 1
shf301
  • 31,086
  • 2
  • 52
  • 86