Hello everyone,

I was wondering how to get a program to run in the background without having it up and have an icon in the tray saying that it is running. I am guessing that it might be included with either system.windows.forms or System.Design or in Microsoft.Win32?
if anyone can help it would be great.
Thanks

Dani AI

Generated

Short summary for : the usual solution is a normal Windows Forms app that hosts a System.Windows.Forms.NotifyIcon (tray icon) and hides the main form when backgrounded. A Windows Service is not the right choice for a UI/tray icon (services run in an isolated session on modern Windows and cannot reliably show a tray UI); the tray icon approach shown by is the correct direction.

A few practical corrections and pitfalls observed in ’s sample:

  • The Click handler in the sample omits braces, so only the first line is conditional; the restore/activate lines run regardless. Make the restore block atomic (use braces) or check state before calling Restore/Activate.
  • Hiding a form is not the same as closing it. To keep the app running when the user closes the window, cancel the close and Hide the form instead (while keeping the NotifyIcon visible).
  • Always Dispose the NotifyIcon on real exit so the icon does not linger.

A compact pattern to intercept close and hide to tray (override form close):

protected override void OnFormClosing(FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        e.Cancel = true;
        this.Hide();
        notifyIcon.Visible = true;
        return;
    }
    base.OnFormClosing(e);
}

A cleaner alternative is to run the app from an ApplicationContext (no visible main form until needed). Example skeleton:

class TrayContext : ApplicationContext
{
    NotifyIcon icon;
    Form mainForm;
    public TrayContext()
    {
        mainForm = new MainForm();
        icon = new NotifyIcon { /* Icon = ..., ContextMenu = ..., Visible = true */ };
        icon.DoubleClick += (s,e) =>
        {
            if (!mainForm.Visible) { mainForm.Show(); mainForm.WindowState = FormWindowState.Normal; }
            mainForm.Activate();
        };
        mainForm.FormClosed += (s,e) => ExitThread();
    }
}

Call via Application.Run(new TrayContext()); from Main.

Quick checklist of best practices: set ShowInTaskbar=false when hidden; put an Exit item in the icon context menu that calls notifyIcon.Dispose(); embed the .ico in resources; use a Mutex for single-instance behavior; split long-running/system tasks into a service and use the tray UI as a controller if elevated/background work is needed.

Recommended Answers

All 8 Replies

Don't quote me on this, but it might have something to do with making it a service, rather than an application.

well, I want it to be able to come back to the top, like it is hiding in the tray waiting for the user to click on it and then it comes to the top.

Well Dark_Omen, your in luck i have been recently been working on the same thing myself for my program, only that i wanted the main program to go directly to tray when i minimised it and disapear from tray when i cliked on icon in tray. Anywho it works fine with me. Probably you might want to look into the background thing with a more experienced programmer, but here is what i've done.

First put a notify icon tool on the form you want to be in tray and program like so

in Windows Form designer put this code in the notifyicon section

this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);

then put in lines of code this:

private void notifyIcon1_Click(object sender, EventArgs e)
		{
			if (this.WindowState == FormWindowState.Minimized)
				this.Show();
				this.WindowState = FormWindowState.Normal;
				this.Activate();
				notifyIcon1.Visible=false; //if you want to remove from tray when clicked on
		}

then enter this code in Form_Load to prevent the form from showing and tay in tray

private void Form1_Load(object sender, System.EventArgs e)
		{
				this.Hide();
		notifyIcon1.Visible=true;
		}

but also make sure you have this in the Windows Form design in the Form area or just change properties to Window State to minimized

this.WindowState = System.Windows.Forms.FormWindowState.Minimized;

I hope this helps you out!
And write back if you cant get it to work
Happy coding!

Ok this is a little confusing for me, I am having trouble finding the places to put the code that you have posted. Do you think you could show me an example program using this.

Thanks

Sure! This is a program that will not work if compiled... Its just there to help you locate the areas where you should include the code.
I hope this helps you out! And concerning making the program run in the background, visit this site How To Create a Thread by Using Visual C# .NET

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace App
{
	public class Form1 : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;;
		private System.Windows.Forms.NotifyIcon notifyIcon1;

		public Form1()
		{
			InitializeComponent();
			notifyIcon1.ContextMenu = this.contextMenu1;
			notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_Click);
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#region Windows Form Designer generated code
		private void InitializeComponent() 
		{
                   this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
                   this.SuspendLayout();
			// 
			// notifyIcon1
			// 
			this.notifyIcon1.ContextMenu = this.contextMenu1;
			this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
			this.notifyIcon1.Text = "Something";
			this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);
                 }
		#endregion
		[STAThread]
		static void Main() 
		{
			System.Windows.Forms.Application.Run(new Form1());
			
		}
		private void notifyIcon1_Click(object sender, EventArgs e)
		{
			if (this.WindowState == FormWindowState.Minimized)
				this.Show();
				this.WindowState = FormWindowState.Normal;
				this.Activate();
				notifyIcon1.Visible=false; //if you want to remove from tray when clicked on
		}
                private void Form1_Load(object sender, System.EventArgs e)
		{
				this.Hide();
		notifyIcon1.Visible=true;
		}

Ok I think I get it. Thanks for the help.

tell me if you hit any snags. I know it may seem confusing but its actually pretty simple. Anywho offer some feedback after trying to implement.

Cheerz!

I wan wondering if this code is the same when we programm in linux? Can you help me with this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.