views:

71

answers:

5

I have created an application using C# 3.0.I need that application to start and run continuously whenever Windows starts.After setting up and installing the application this process should happen. Please give your suggestions to do this.

+3  A: 

Make it a Windows service, and have it start automatically. Or, if you only care when users are logged in, put it in the shared startup folder so it'll start for every user.

Jimmy
A: 

What about adding the program to the start-up folder in the install? When you create your set-up project, just add a shortcut to the output from the main program to the destination PC's start-up folder.

pm_2
How to create a shortcut of our application for the PC's start-up folder using the Visual Studio Setup and deployment project?
Abilash
Right click the set-up project, select View File System, and you should be presented with a view of the destination file system. On the left hand side you have "File system on target machine", right click there and select "Add special folder"->"Startup Folder"
pm_2
Thank you pm_2,this solved my problem
Abilash
+4  A: 

If you need an app to start when Windows starts and run continually, it should be a Windows Service. You won't be able to have GUI and Windows as a part of the same app/project, you'd need to write that separately and communicate with the Service.

If you want it to run continually and not stop, a plain Windows Forms app won't do it: the user can close it, of course. They can close a service too but you can configure in (on the Recovery tab) to restart if it 'fails' (is killed in Task Manager). They can still stop the service manually, though - but not as easily.

It's a project template built into Visual Studio. Depending on what your app is doing, it's usually best to create an instance of a Thread object, and put your logic in a loop of sorts that executed on the new thread. From the OnStart and OnStop methods of the Service thread itself, you can create the thread, or signal it to stop (or simply Abort it).

By right-clicking on the service in the designer, you can 'Add Installer', which means if you run installutil with the name of the application, the service will be installed and, if you set the correct properties on the Service and Installer, run automatically.

In terms of if you need a user interface - you'd have to come up with some way of the UI to talk to the service; you could use Named Pipes, Memory-Mapped Files, or you can use WCF (.NET 3.5+) with the Named Pipes/TCP provider, and call right into it.

For reference:

http://msdn.microsoft.com/en-us/library/aa984464(VS.71).aspx

Hope that helps.

Kieren Johnstone
I dont know any thing about Windows Service..how can i make my user interface application to startup.can you please write the code..
Abilash
Yeah c'mon Kieren, write his code for him :)
AndrewS
@Abilash: Me neither, but as a programmer you are supposed to learn new stuff :) Try: http://www.google.com/search?q=learn+Windows+Service+c%23
Peter Forss
Thanks Peter, i ll go through this stuff..
Abilash
A: 

What installation app are you using? Often they have functions for this built in.

If you want to do it in your c# code you will have to edit in the registry. Read this article. (I guess you also have to require administrator rights in the manifest to do that)

Peter Forss
Visual Studio Setup and deployment project
Abilash
A: 

Use WindowService in that OnStart() {......} you will write code to start your application

sankar
You should *not* put all of your code in `OnStart`. The `OnStart` method should start the service then return.
Kieren Johnstone