2

I'm doing a windowsService program, which is take screen capture automatically every A sec and set it on dir. When I run it works only one time. How can I make it? Here is my code.

protected override void OnStart(string[] args)
{
    timer1_Tick();
}
private void timer1_Tick()
{
    string myDir = "c:\\Newfolder\\photo";
    System.IO.Directory.CreateDirectory(myDir);
    Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);
    Graphics graphics = Graphics.FromImage(bitmap as Image);
    graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
    string fileName = string.Format(@"c:\Newfolder\photo\Screenshot" +"_" + DateTime.Now.ToString("(dd_MMMM_hh_mm_ss_tt)") + ".png");
    bitmap.Save(fileName, ImageFormat.Png);
}
No Name
  • 33
  • 6
  • 2
    on start you call `timer1_Tick` once. don't do that - start timer instead: `timer1.Start();` it will tick and call `timer1_Tick` each time – ASh Feb 05 '16 at 07:16
  • 2
    Sorry my English a litlle bad ! you wanna say like this #protected override void OnStart(string[] args) { timer1.Start(); timer1_Tick(); }# – No Name Feb 05 '16 at 07:24
  • yes, right. we still need initial call before A sec passed – ASh Feb 05 '16 at 07:30
  • See also: [My answer on Capture screen on server desktop session](http://stackoverflow.com/a/12851218/402022) – Theraot Feb 05 '16 at 07:51

2 Answers2

1

In your OnStart method remove the call to timer1_tick and add this instead:

timer1.enabled = true;
timer1.interval = 10000; //change this to whatever you need
timer1.Start();

This is provided you have already attached the tick event to the timer.

Vivek Verma
  • 333
  • 3
  • 13
  • 1
    Yes I know this but , when I run It call OnStar method only one! – No Name Feb 05 '16 at 07:42
  • 1
    OnStart method is the service initializer. It will always run only once. – Vivek Verma Feb 05 '16 at 07:44
  • @NoName The method `OnStart` is expected to run only once. Instead of calling `timer1_Tick` you could set the `timer1` to execute `timer1_Tick` multiple times. If you did set the `timer1` and still have problems with your event, it may be the case that the method `timer1_Tick` is not correcrly bound to the Tick event of `timer1`. In that case, see [My answer to How to change the name of an existing event handler?](http://stackoverflow.com/a/20734571/402022) – Theraot Feb 05 '16 at 08:01
1
static void Main()
    {if DEBUG
        Service1 Myservice = new Service1();
        Myservice.OnDebug();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);else
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new Service1()
        };
        ServiceBase.Run(ServicesToRun);endif
    }
No Name
  • 33
  • 6
  • 1
    While it is ok to answer you own question, this is not an answer. If you want to add information to your question you can edit it. Also, this site is not a forum - you have already taken the tour, you should have a graps of that. You can remove this answer at any time. – Theraot Feb 05 '16 at 07:57
  • 1
    Ok. I know Theraot, firstly I saw your answer after add information and I'm new in this site so I don't have a enough information about this site ok. – No Name Feb 05 '16 at 08:06
  • 1
    Also my english litlle bad ok – No Name Feb 05 '16 at 08:07
  • 1
    I understand you are new. That's why I'm telling you. The intention is to keep the site clean. In particular the ideal is: one question on top (with all the details needed), various answers follow. The order of the answers may change. Comments are mainly for clarifications. Also, we encourage quality answers. Edit: comments can be edited too, as long as they are not old enough. – Theraot Feb 05 '16 at 08:11
  • 1
    Ok. Thank you very much @Theraot. I owe you :) ! I try to do this on high temp , if I make a mistake beforehand I'm so sorry – No Name Feb 05 '16 at 08:58