0% found this document useful (0 votes)
69 views4 pages

System Tray Icon With Windows Service

Uploaded by

sestidavide.mail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views4 pages

System Tray Icon With Windows Service

Uploaded by

sestidavide.mail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

23/5/2019 System Tray Icon with Windows Service

A Developer.com Site

CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

Custom Search User Name Password Log in Help Register


Remember Me?

Forum What's New? Advertiser Disclosure

New Posts FAQ Calendar Forum Actions Quick Links Advanced Search

Forum C# Programming C-Sharp Programming System Tray Icon with Windows Service

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register or Login before you can post: click the register link
above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Results 1 to 10 of 10
Click Here to Expand Forum to Full Width
Thread: System Tray Icon with Windows Service

Thread Tools Display


On-Demand Webinars (sponsored)
April 15th, 2006, 11:22 PM #1
→ *new* Replace Your Oracle Database and
Join Date: Jan 2004 Deliver the Personalized, Responsive Experiences
mkennedy Customers Crave
Location: CO
Junior Member Posts: 4 → *new* Datamtion's Comprehensive Guide to
Cloud Computing
→ Unleash Your DevOps Strategy by Synchronizing
System Tray Icon with Windows Service Application and Database Changes
→ Build Planet-Scale Apps with Azure Cosmos DB
Greetings, in Minutes
→ It Might be Time to Upgrade Your Database If...
I've been able to place an icon in the System Tray with my Windows Form but am unable to use the same code in my Windows
Service, any thoughts?

Thanks,

--Mike

notifyIcon = new NotifyIcon();


notifyIcon.Text = "CodeOne SS";
notifyIcon.Icon = new Icon("c:\\WINDOWS\\HRB.ico");
notifyIcon.Visible = true;

Reply With Quote

April 16th, 2006, 04:50 AM #2

Join Date: Jun 2004


Shuja Ali Location: Kashmir, India
Elite Member Posts: 6,808
Power Poster

Re: System Tray Icon with Windows Service


Why would you put a NotifyIcon in a Windows Service?

Services usually do not have any interface, why do you want to add an interface to them?

Reply With Quote

April 16th, 2006, 09:02 AM #3

Join Date: Jan 2002


darwen Location: Scaro, UK
Elite Member Posts: 5,940
Power Poster

Re: System Tray Icon with Windows Service


In actual fact this is very very difficult to do with a .NET service as it doesn't have a windows message loop - which is required for
a tray icon.

It's better to have a seperate winforms application with a tray icon which communicates with the service and provides features
such as start/stop etc. This is how MSDE does it for instance.

Darwen.

www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Reply With Quote

April 16th, 2006, 09:27 AM #4

Join Date: Jan 2004


mkennedy Location: CO
Junior Member Posts: 4

Re: System Tray Icon with Windows Service


Thanks for te reply. I have a requirement from my customer that wants to visually know when/if the service is running. I don't
want to deploy another app to my client PCs to just monitor my service; there must be a better way.

forums.codeguru.com/showthread.php?383810-System-Tray-Icon-with-Windows-Service 1/4
23/5/2019 System Tray Icon with Windows Service

--Mike

Reply With Quote

April 17th, 2006, 07:15 AM #5

Join Date: Jan 2002


darwen Location: Scaro, UK
Elite Member Posts: 5,940
Power Poster

Re: System Tray Icon with Windows Service


After some investigation the easiest way of doing this is to have a seperate thread which calls
System.Windows.Forms.Application.Run on a form with a notify icon attached.

You'll need to change the service settings to allow "interact with desktop" as well as adding the System.Windows.Forms.dll to your
service as a reference.

Example :

Code:
public class WindowsMessageLoopThread
{
private Thread _thread = null;
private AutoResetEvent _startedEvent = new AutoResetEvent(false);
private Form1 _form = null;

public WindowsMessageLoopThread()
{
_thread = new Thread(new ThreadStart(ThreadFunction));
_thread.Start();
_startedEvent.WaitOne();
}
public void Stop()
{
_form.BeginInvoke(new MethodInvoker(_form.Close));
_thread.Join();
}

private void ThreadFunction()


{
_startedEvent.Set();
_form = new Form1();
System.Windows.Forms.Application.Run(_form);
}
}

You should instansiate this class in the OnStart method of your service class (it should be a member of this class) and call Stop in
the OnStop method.

To make the form invisible I usually move it to off the screen and make it invisible in the taskbar e.g.

Code:
this.Bounds = new Rectangle(-this.Width * 2, -this.Height * 2, 1, 1);

I'm including a project which should show you how to do it.

This includes code to allow InstallUtil to set the "interact with desktop" setting properly.

Darwen.

Attached Files
TestServiceWithMessageLoop.zip (10.7 KB, 1916 views)

Last edited by darwen; April 17th, 2006 at 12:34 PM.

www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Reply With Quote

April 17th, 2006, 03:56 PM #6

Join Date: Jan 2002


darwen Location: Scaro, UK
Elite Member Posts: 5,940
Power Poster

Re: System Tray Icon with Windows Service


I've modified the service code so that it survives the user logging off and logging back in again.

See, I told you this wasn't easy... aren't I nice guy investigating all of this for you ?

Basically you need to handle both the Microsoft.Win32.SystemEvents.SessionEnding and the


Microsoft.Win32.SystemEvents.SessionEnded events to re-start the UI thread.

This is dangerous - it might not work under .NET 2.0 or under Windows Vista, but it seems to work ok for XP and .NET 1.1.

My preferred route, and the one I would normally use in my job, is to have a seperate monitoring application which is run at
startup of a user login.

You wanted this though and I've taken it as far as I can do.

Edit: Just tried it under VS2005 and it works fine... wow

Hmmm, I feel like an article possibility coming on here.

Darwen.

forums.codeguru.com/showthread.php?383810-System-Tray-Icon-with-Windows-Service 2/4
23/5/2019 System Tray Icon with Windows Service

Attached Files
TestServiceWithMessageLoop.zip (11.0 KB, 995 views)
TestServiceWithMessageLoop2005.zip (12.7 KB, 1528 views)

Last edited by darwen; April 17th, 2006 at 04:13 PM.

www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Reply With Quote

April 17th, 2006, 09:08 PM #7

Join Date: Jan 2004


mkennedy
Location: CO
Junior Member Posts: 4

Re: System Tray Icon with Windows Service


Excellent, works great. I like to idea of a forms app that monitors the service(s). Thanks again.

Reply With Quote

September 11th, 2008, 04:34 AM #8

Join Date: Sep 2008


Yolk_gf
Posts: 2
Junior Member

Re: System Tray Icon with Windows Service


Hello

I took your VS2005 project (thanks! it's exactly what i was looking for) and converted it in VS2008.

On XP it works great I can use it in Debug and install it, all ok( can see the Notify Icon and use it to show and hide the window).

But when I install the Service on Vista it starts but the Notify Icon doesn't appears. Maybe you know the reason?

Thanks for advance.

Reply With Quote

September 11th, 2008, 12:25 PM #9

Arjay Join Date:


Posts:
Aug 2004
12,975
Moderator / MS MVP
Power Poster

Re: System Tray Icon with Windows Service

Originally Posted by Yolk_gf


But when I install the Service on Vista it starts but the Notify Icon doesn't appears. Maybe you know the reason?
The 'interact with the desktop' service setting was a security hole. As such Microsoft dropped support for this on Vista and newer
OS's.

To get around this problem you can create a separate tray icon application that gets started when the user logs on. The tray icon
app communicates with the service using a WCF service (which can be hosted within the Windows service).

My Code Guru Articles

Reply With Quote

September 11th, 2008, 03:33 PM #10

Join Date: Jan 2002


darwen Location: Scaro, UK
Elite Member Posts: 5,940
Power Poster

Re: System Tray Icon with Windows Service


I love writing stuff which people don't read :

it might not work under .NET 2.0 or under Windows Vista


and

My preferred route, and the one I would normally use in my job, is to have a seperate monitoring application which is run at startup
of a user login.
Darwen.

www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

forums.codeguru.com/showthread.php?383810-System-Tray-Icon-with-Windows-Service 3/4
23/5/2019 System Tray Icon with Windows Service

Reply With Quote

Quick Navigation C-Sharp Programming Top

« Previous Thread | Next Thread »

Posting Permissions
You may not post new threads BB code is On
You may not post replies Smilies are On
You may not post attachments [IMG] code is On
You may not edit your posts [VIDEO] code is On
HTML code is Off
Forum Rules

-- Blue Codeguru Contact Us CodeGuru Forums Privacy Statement Top

Acceptable Use Policy

A Developer.com Property

Terms of Service | Licensing & Reprints | About Us | Privacy Policy | Contact Us | Advertise |
Copyright 2019 QuinStreet Inc. All Rights Reserved.

Advertiser Disclosure: Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on
this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.
All times are GMT -5. The time now is 11:03 AM.

Copyright Quinstreet Inc. 2002-2017

forums.codeguru.com/showthread.php?383810-System-Tray-Icon-with-Windows-Service 4/4

You might also like