Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.10/5 (7 votes)
I created one windows application in c#, now i would like run that application when the user starts the system,but the application have few settings to run that application for example button click to start sometime.so how can i run that application in the background
Posted
Updated 6-Sep-17 22:16pm
v3
Comments
[no name] 10-Mar-15 7:16am    
Please clarify: What do you mean by "running it in the background"?
Srikanth59 10-Mar-15 8:07am    
i need to run the application in the background in the windows,it does the work without disturbing the user
[no name] 10-Mar-15 8:25am    
You could create a link in the Autostart-Folder to let it start automatically on user-logon. And minimize its window by default.
074 Manoj kumar 29-Mar-24 3:23am    
That means your application was running even it's not open like whatsapp
Kornfeld Eliyahu Peter 10-Mar-15 8:25am    
Turn the application into a service maybe...

Create a Windows Service rather than an application, they run in the background and can start automatically

Also note; windows services have a priority startup time above all other standard form based applications, and they can also be run before a user logs into windows itself.

You can get started by following the MSDN docs listed here
How to: Write Services Programmatically
 
Share this answer
 
v2
Comments
[no name] 5-Apr-16 13:11pm    
Rating of 5 as this is more practical and professional approach to the other solutions. Also improved your answer to add more clarity.
kbhtech 8-Aug-16 7:14am    
I strongly agree with that concept, creating windows services provides a better means of managing background processes while the service may also utilize a forms object (or, custom class for that matter) in the notification tray.

Consequently, we are looking at a service with onStart, onStop, onPause, onResume, and onRestart events which allow for easy manipulation of the program depending on the systems status.

Then, by including a forms application in the service, which is tucked away in the System Tray: we have enabled even more management, configuration, and run-time manipulation without the need of writing a client. By which, the notification Tray Icon does not even have to show unless some sort of process has taken place.

Sincerely, do not get hung up on manipulating a service while on the job. Whenever you get home though research it and make a service which has a system tray icon embeded into it while initiated invisibly.

Then, create some sort of application which will report to the service in order to show the System Tray Icon and the form: for management reasons.
What I understand: You want to run your application at windows startup.

What you can do:

1) Click on Start Menu, select All Programs, and locate Startup folder, right-click, and click on Open.

2) Make a shortcut linked to your program and move it to Startup folder.

3) If you don't want to show your application, open your solution, go to your designer, select the Form1 or whatever it's called, and go to Properties Panel, change "Show Icon" option to "False", finally, set WindowState to "Minimized".

That's it. When you run Windows again, your application should automatically start after the user log.

Hope this helps! Cheers! - CCB
 
Share this answer
 
Comments
[no name] 5-Apr-16 13:23pm    
This is a very sloppy way to create application startup parameters at windows boot. If the OP is using install-shield, they can derive this functionality from install-shield directly. Further and more practical solutions can be served from a simple service/service controller which would be more beneficial in my opinion.
kbhtech 8-Aug-16 7:20am    
Sloppy, an entitled opinion:

By using such procedure, the background process cannot change dependent on the state of the operating system which may render the program as bugged. This would not be a horrid solution, had it not been that for a little less effort a fully functional and bug proofed process can be created.

Therefore, not only is the inefficient at performance but it is also inefficient with time. Consequently, this method is inefficient period.

However: If the process is to close once a procedure has taken place... well even then this would be inefficient as services do allow manipulation VIA Task Scheduling.

So, It's "sloppy" if that is not enough this is why.
I guess you want to hide the application you want to run right?
Well here's my solution:
1st. You need to start it right after the windows start up.
This is the code in adding your software/program run right after the windows start-up:
C#
//adding to microsoft startup program
//using Microsoft.Win32;
using (RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
    reg.SetValue(ProjectTitle, Defaultpath + ProjectName);
}

//removing to startup program
/*using (RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
    reg.DeleteValue(ProjectTitle,false);
}*/


2nd. And you need to hide the forms in code and your program is running background already, and its up to you what you want to do it just happen that you want to run your program background.

and here the code to hide your Form:
this.Hide();


hope it'll help you sir.
 
Share this answer
 
There are coupe of ways exist to accomplish your task
1. create a windows service and deploy it for automatic run, it usually run in background
2. create a windows/console application with background worker class and keep that exe in users\startup folder so when user gets logged in it will call that exe on startup of each user
 
Share this answer
 
add a systrayNotifyIcon component on your form
add a contextMenuStrip component on your form
in the systrayNotifyIcon properties, set the ContextMenuStrip to the contextMenuStrip component you just added on the form.
Select the ContextMenuStrip component and you will see a menu system at the top of your form, this menu system will be the system showing when the user right click on the icon in their systray.
Create the menus as you normally do and create methods for them to do stuff, what ever it is.

Then on your form load, you need to create and initialize the doubleclick event for the systrayNotifyIcon and also add an icon (be sure to add an app icon for your application), and do something like this




C#
this.theSystrayNotifyIcon = new NotifyIcon(this.components);
this.theSystrayNotifyIcon.Icon = new Icon(this.Icon, this.Icon.Width, this.Icon.Height);
//this.theSystrayNotifyIcon.ContextMenuStrip = this.theContextMenuStrip;
this.theSystrayNotifyIcon.Visible = true;
this.theSystrayNotifyIcon.DoubleClick += new EventHandler(theSystrayNotifyIcon_DoubleClick);
..
..
 
private void theSystrayNotifyIcon_DoubleClick(object sender, EventArgs e)
{
   if (this.Visible)
   {
      this.Hide();
   }
   else
   {
      this.Show();
      this.BringToFront();
      this.Activate();
   }
}



may work ...

refered from : https://social.msdn.microsoft.com/Forums/windows/en-US/30fc344e-a24c-4259-812b-cfd35f7c0d0a/how-can-i-make-a-program-run-in-the-background?forum=winforms[^]
 
Share this answer
 
v2
Comments
[no name] 5-Apr-16 13:16pm    
This is not what the OP asked. Nor does it show or provide any clarification on launching his application silently through windows.
I've seen a strong proclamation of services, therefore I'm going to provide a different solution.

Console Application, which requires arguments and executes different tasks based on arguments. Make it hide itself. Then, go into Control Panel > Administrative Tools > Task Scheduler.

Do not, have the console wait for input to exit. It should just execute args and close. This is yet, another method which is clean.
 
Share this answer
 
Recently, I have worked on similar requirement and below is what I did.

1. First add your application to start up using below code

<pre lang="c#">
private void AddApplicationToStartUp()
        {
            RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            reg.SetValue("Your App Name", Application.ExecutablePath.ToString());
        }


2. Upon start up run application in background as below

C#
    public void RunFormInBackGround()
{
    this.WindowState = FormWindowState.Minimized;
    ShowInTaskbar = false;
    this.Visible = false;
}


3. I'm executing methods mentioned in Point # 1 and 2 in form load event as below

C#
private void frmBackground_Load(object sender, EventArgs e)
        {
            RunFormInBackGround(); 
            AddApplicationToStartUp();

        }


I hope above helps, for further queries / questions, do reply back.
 
Share this answer
 
If you have access to modify registry details.
You could use this code

RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
reg.SetValue("Your App Name", Application.ExecutablePath.ToString());


If you dont have access to modify registry details, us the below one
1. On Load Event write this code:
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;

2. Using Application.ExecutablePath, copy your exe file to the start up folder using System.IO.File.Copy
 
Share this answer
 
Comments
Mohideenmeera 29-Dec-16 8:38am    
Did any of the given recommendations worked. If yes, pls mark as answered...
CHill60 29-Dec-16 9:24am    
The post is nearly 2 years old. I advise sticking to providing answers to more recent posts, and preferably ones that don't already have appropriate solutions. As you see, many of your solutions are being downvoted and reported.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900