run application in back ground

hi all

i have prepared an application which dose not take any user input and the aim of the application is to take screenshots at a regular interval of 5 min

i am using vb.net 2005

for this i have taken a form and in that form i have placed a timer control and in timer trick i am calling the function which takes the screen shot

my requirement is to set up this application and this should run in the back ground and the user should not be able to cancel or stop the running of the application

can any one suggest me how to do this the application should run in the background it self and this should start when ever the system boots

[674 byte] By [Rajeshbatchu] at [2007-12-28]
# 1

Hi,

you'll want to write a Windows Service application. Take a look at this HowTo article on MSDN for detailed explanation and implementation steps.

Andrej

AndrejTozon at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

I would say a service is not the right approach for this - since the implementation requires a logged in user to function. Make it startup when the user logs on, put an icon in the system tray to allow the user to start/stop the capture, and have your actual program have no interface (or a simple 'about' box, so they know what the icon is actually doing). If you already have the form, simply don't show it (use a 'sub main' function to start you application with the form invisible).

The application shortcut can be placed in the startup folder or the 'run' registry key.

So you need to add several things:

  • Hide the form on startup - use sub main and Application.Run(MyForm)
  • Create a system tray icon, with a context menu
  • Add the program shortcut to the Run registry key or startup folder - this is usually done at installation, which means you need an installation package.
SJWhiteley at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

You don't necessarily want the user to be logged on... To allow service to interact with the desktop, you'll have to check the "Allow service to interact with desktop" option in your service's options. Then, program your service application, that it only takes snapshots if a user is logged on...

Doing it this way, the service will always starts on windows boot and will take desktop snaps no matter who is logged on... And it would act invisible for a regular desktop user...

Andrej

AndrejTozon at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

I agree, a service is the way to do this kind of app.

You should realize that anyone with local administrative access on the computer and with a little bit of knowledge about how Windows works will be able to disable the program rather easily. The only way to prevent this without removing local admin access from your users would be to go a lower level route such as a rootkit. Even this can be removed although it is quite a chore. I wouldn't recommend this unless your project goal is to create a nasty spyware/malware app.

FrankCarr at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5

If you need to take screenshots for multiple users, then you can, of course write this as a service. I would still say that such an application should not be a service - we have far too many services as it is. Particularly an application which takes screenshots is not necessarily a user-friendly application.

It really depends on the application. What you have described doesn't necesarily mean 'service' you have to decide if it's appropriate. If the reason to run it as such is to prevent a user from disabling it, then you have more serious problems.

SJWhiteley at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

i have decided to make it as a service

in the service i have taken one timer control from the component tab and in time trick event i have called the function which takes the screen shot

and in service start i have decided set timer1.enabled="True"

and in service stop i have set timer1.enabled="False"

but my problem is the timer is not workin in my service

as we cannot debug the service i am un able to see weather the service timer trick is firing or not

but i am sure that service is getting started

preparing a service is a good idea but timer control is not working in the service

Rajeshbatchu at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7

Imports System.IO

Imports System.Drawing

Imports System.Windows.Forms

Protected Overrides Sub OnStart(ByVal args() As String)

' Add code here to start your service. This method should set things

' in motion so your service can do its work.

Timer1.Enabled = True

End Sub

Protected Overrides Sub OnStop()

' Add code here to perform any tear-down necessary to stop your service.

Timer1.Enabled = False

End Sub.

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

screenshot()

End Sub

i am using the above code in the visual studio 2005 in the windows service

but unfortunately the timer control is not getting activated

and the function screenshot() is not getting run

as i cannot see the debuging of service i am unable to track where the problem is

can any one help in this please

Rajeshbatchu at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8

Sorry I can't help, but I would be real interested to see the code for the screenshot function.

ScottB33 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9

Rajesh,

you'd have to use a different kind of timer, e.g. System.Threading.Timer... here's the complete service code I've quickly put together...:

Imports System
Imports
System.Drawing
Imports
System.Drawing.Imaging
Imports System.Windows.Forms

Public Class ScreenLoggerService
Private myTimer As
Threading.Timer
Private timerDelegate As Threading.TimerCallback

Protected Overrides Sub OnStart(ByVal args() As String)
timerDelegate =
New Threading.TimerCallback(AddressOf
Tick)
myTimer =
New Threading.Timer(timerDelegate, Nothing
, 0, 10000)
End Sub

Protected Overrides Sub OnStop()
myTimer.Dispose()
End Sub

Private Sub Tick(ByVal state As Object)
Dim image As New
Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb)
Dim g As
Graphics = Graphics.FromImage(image)
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)
image.Save(
"c:\" + DateTime.Now.ToString("yyMMddhhmmss") + ".png"
, ImageFormat.Png)
End
Sub
End
Class

In the above code, the screen snapping interval is set to 10 seconds (10000 ms). Change this value in the "my Timer = New Threading..." line.

Hope this helps,

Andrej

AndrejTozon at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 10

this is showing a following exception of "the handle is invalid"

when i start the service

in the following line

g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)

the exception details are as follows

please help me in solving this exception

System.ComponentModel.Win32Exception was unhandled
ErrorCode=-2147467259
Message="The handle is invalid"
Source="System.Drawing"
StackTrace:
at System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize, CopyPixelOperation copyPixelOperation)
at WindowsService1.ScreenLoggerService.Tick(Object state) in C:\Documents and Settings\brajesh\Desktop\WindowsService1\WindowsService1\Service1.vb:line 22
at System.Threading._TimerCallback.TimerCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading._TimerCallback.PerformTimerCallback(Object state)

Rajeshbatchu at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 11

Make sure you checked the "Allow service to interact with desktop" option on your service's Log On option tab (Log On as Local System account), before you started the service.

Andrej

AndrejTozon at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 12

please help me to solve this problems

1. i should make the service run automatically what should i do

2.is it necessary to login as the admininstrator in order to install the service

3. my requirement is to make the service run when any user login to the system

what i have done is

i have installed the service and i am getting the screen shots

but

each time when i shutdown and restart the service is not running automatically

and

i am not getting the screen shots of the other user who is using this system and loged on with his user name and passwword

Rajeshbatchu at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 13

Could it be that your service is crashing? Try wrapping your screensnapping code with the Try...Catch block. To make the Service run automatically, set its startup type to Automatic.

Andrej

AndrejTozon at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 14

i have installed the screenlogger service in my login it is working fine and i am not the administrator of the system

but

when some other user login with different username and password the screen logger is not working

i have kept the service start type to automatic but it is not starting automatic when ever the system is booted

can i prepare a set up file to my screenlogger service so that it will gets installed like other window application programs if so how

Rajeshbatchu at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...