System Tray Icon With Windows Service
System Tray Icon With Windows Service
A Developer.com Site
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
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
Thanks,
--Mike
Services usually do not have any interface, why do you want to add an interface to them?
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.
forums.codeguru.com/showthread.php?383810-System-Tray-Icon-with-Windows-Service 1/4
23/5/2019 System Tray Icon with Windows Service
--Mike
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();
}
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);
This includes code to allow InstallUtil to set the "interact with desktop" setting properly.
Darwen.
Attached Files
TestServiceWithMessageLoop.zip (10.7 KB, 1916 views)
www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.
See, I told you this wasn't easy... aren't I nice guy investigating all of this for you ?
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.
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)
www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.
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?
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 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
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
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.
forums.codeguru.com/showthread.php?383810-System-Tray-Icon-with-Windows-Service 4/4