954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Program running in the background and in the tray

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

Dark_Omen
Posting Pro
573 posts since Apr 2004
Reputation Points: 23
Solved Threads: 6
 

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

Iron_Cross
Junior Poster
117 posts since Jul 2003
Reputation Points: 46
Solved Threads: 2
 

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.

Dark_Omen
Posting Pro
573 posts since Apr 2004
Reputation Points: 23
Solved Threads: 6
 

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!

crypter
Light Poster
37 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

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

Dark_Omen
Posting Pro
573 posts since Apr 2004
Reputation Points: 23
Solved Threads: 6
 

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;
		}
crypter
Light Poster
37 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

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

Dark_Omen
Posting Pro
573 posts since Apr 2004
Reputation Points: 23
Solved Threads: 6
 

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!

crypter
Light Poster
37 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

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

Kapatsos
Newbie Poster
1 post since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You