Solved

Tray Notification Icon in windows service

Posted on 2010-08-26
24
1,057 Views
Last Modified: 2012-05-10
Hi,

 I have facing two problems now. First i can not run the windows service in my system. I put the setup and deployment for my application for my windows service. It doesn't shows in the Services and Applications. I put the installutil.exe in the particular application folder and run it. Again it doesn't show in the service and application.This is my problem.
       Also once the services run, i need to show tray icon and i double clicked it will open the form applciation. Is it possible using services while automatically services run in the system?

Hope your's reply.

Thanks
0
Comment
Question by:Rajkumar Gs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
  • 4
  • +1
24 Comments
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33539253
Hi,

First i can not run  the windows service in my system. I put the setup and deployment for my  application for my windows service.
So I am assuming that you are having Setup of service with you. You have installed it but still services are not visible in Services window or are you not able to check if service is running or not. Or if I am wrong let me know what you are doing?

 Also once the  services run, i need to show tray icon
As far I know about Services they do not have any user interface so that might be not possible in that case.

Regards,
VSS
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33539355
Hi ,

  In my first case i got the services and its started. I got it. But in second case why it is not possible for tray notification icon? Is nothing have the solution for this one? Is any idea about this?

Hope your's reply.
Thanks
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33539360
Hi,

I forgot to tell another thing. I have a requirement from my customer that wants to visually know the icon and double click it will show one windows form, 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.

Thanks
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33539364
Hi,

I just mentioned might be. And I too believe that, To each and every problem there is a solution until we do not find it out.

I'll give it try & let you know within short span of time.

Regards,
VSS
0
 
LVL 4

Expert Comment

by:kjetilroe
ID: 33539389

Technically what you are describing is not a windows service, It is a windows forms application. As soon as you want to use a userinterface it is no longer a windows service but forms, something that is actually making your life much easier. I advice you to make an application with a limited or mostly hidden userinterface. You can still use either windows scheduler or some other way of automatically starting it, and keeping it running in the background.
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33539442
Hi,

Thanks for your's reply. Ok suppose i have use form application and using this app in my windows services. I got a code that is

namespace WindowsMessageLoopThread
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        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);
            }
        }
    }
}

This is windows form application. I give a icon path in the properties and run this application the icon appeared in the tray. But form application not have a dll.How can i give a reference to this services. I got the code in the below link:

http://www.codeguru.com/forum/archive/index.php/t-383810.html

If any hint, hope your's reply.

Thanks
0
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 33539984
Typical usage for a Windows Service is to make it run automatically when the computer starts, even without anyone logging on to the machine.  You can't do that with a Windows Forms App.

However, you can write another windows forms app to "monitor" the service, maybe communicate with the service (WCF or .NET Remoting), and that app can have a tray icon and a form.

You can't have both in 1 single exe or dll.
0
 
LVL 4

Expert Comment

by:kjetilroe
ID: 33540041

Actually forms applications can use assemblies or a dll. 90% of the code in a windows forms application usualy reside in dlls. Technically you have several options for communicating between your windows service and your forms application.

1. The old DDE technology, even if it starts to become quite outdated.
2. COM / COM+
3. TCP server / client, then you also can watch your service from other computers.
4. SharedMemory - Ultra fast. Just make sure that the forms app is only reading, and service is only writing. If you want to exchange information between the applications Separate the areas for the different applications.
5. Just update a file in your filesystem. Just make sure that your service user have write access to the file.


0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33540071
Hi,

Thanks for your reply. I crossed the two problems and find out the solution as per your guidance. Actually you are right. I created windows form application and give the path using,

System.Diagnostics.Process.Start("\\WindowsMessageLoopThread.exe"); This working fine that once i start the services the form displayed and the icon placed in the tray notify.

 I have one question that i dont want to do this. I want to click the tray icon and the windows form need to open.Is it possible?

Hope your's reply.

Thanks
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33540102
Hi,

Check code at link:
http://niitdeveloper.blogspot.com/2010/08/shoot-your-screen-by-screenshot.html

for the solution you need for your last comment.

Regards,
VSS
0
 
LVL 4

Expert Comment

by:kjetilroe
ID: 33540128

Here are also an example of minimizing windows to the tray:

http://www.codeproject.com/KB/cs/Trayminimizer.aspx

0
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 33540137
Do you mean the "tray" (lower right corner, small icons next tot the clock) or the "taskbar" (where you have a larger "button" for most open applications)?

In the last case, you can start the Form minimized.
In the first case, you need to add a "NotificationIcon" to your form, and then write code to handle mouseclicks on that icon.
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33540522
Hi,

Thanks for your's reply.  while the mouse click the event is not fired. It showed in the tray once i click the tray icon the second form is not opened.

 private void notifyIcon1_MouseClick(System.Object send, System.Windows.Forms.MouseEventArgs e)
        {
            ShowNewForm SecondForm = new ShowNewForm();
            SecondForm.Show();
        }

Thanks
       
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33540556
Hi,

May I know which link you used for reference?

Regards,
VSS
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33540605
Hi,

 i am referring msdn . The event is not fired while i clicked the icon in the tray icon

Thanks
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33540634
Hi,

 I put my code here

using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NotifyIcon
{
 
    public partial class QuNotifyIcon : Form
    {
       
       
        public QuNotifyIcon()
        {
            InitializeComponent();
        }
        public class NotifyIcon
        {
            private Thread _thread = null;
            private AutoResetEvent _startedEvent = new AutoResetEvent(false);
            private QuNotifyIcon _form = null;

            public NotifyIcon()
            {
                _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 QuNotifyIcon();
                System.Windows.Forms.Application.Run(_form);
            }
            public static void ThreadProc()
            {
                Application.Run(new QuNotifyIcon());
            }
        }


        private void notifyIcon1_DoubleClick(System.Object send, System.EventArgs e)
        {
            ShowNewForm SecondForm = new ShowNewForm();
            SecondForm.Show();
        }

        private void QuNotifyIcon_Load(object sender, EventArgs e)
        {
            QuNotifyIcon Firstform = new QuNotifyIcon();
            Firstform.Show();
        }
       
    }
}

In single application i put two forms. In loading i need to hide the first form. Once the icon visible in the tray and i double click the icon, the second form need to show. what fault i done in this code. Please review my code. If any mistake i done , note it me.
Also i attached image file of my output screen. In the scree look at my taskbar. How many no of icons loaded

Thanks
trayicon.JPG
0
 
LVL 4

Assisted Solution

by:kjetilroe
kjetilroe earned 200 total points
ID: 33540807

It also seems like you forgot to hook up the event to the notifyIcon object.

            this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);

Dont create a new form that also creates the same notifyicon once again. Use the one form you are programming from to avoid getting into everlasting loops of creation of notifyIcons.

Here is another example:
http://www.codeproject.com/KB/dotnet/notifyiconcontrol20.aspx



0
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 300 total points
ID: 33540983
Hi,

For your exception as shown in snapshot, refer to link:

(1) http://www.aboutmydot.net/index.php/unhandled-exception-win32exceptionerror-creating-window-handle

(2) http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/3fb91a63-a86a-4817-9c3a-5b2f2443926f

They say that you are having lot of controls on form and so window handles are not available for creating those other controls.

And why you are calling same form object again from same form as in code:

private void QuNotifyIcon_Load(object sender, EventArgs e)
        {
             QuNotifyIcon Firstform = new QuNotifyIcon();
             Firstform.Show();
        }

And last I would again tell you to have look on code at link:
http://niitdeveloper.blogspot.com/2010/08/shoot-your-screen-by-screenshot.html

where in code the starting form is minimized and then the same could be regained back by clicking on notification icon.

Regards,
VSS
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33541300
Hi guys,

Thanks for your's reply. Finally i need one scenario. I double click the notify icon the form is opened. Again i double clicked again form opened that is multiple times i clicked the notify icon, multiple forms (same form opened multiple times) opened.

Is there any solution for this one?

Hope your's reply.
Thanks
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33541465
Hi,

Please See code.

Regards,
VSS

//Declare form as 
ShowNewForm SecondForm=null;

private void notifyIcon1_DoubleClick(System.Object send, System.EventArgs e)
        {
           if(SecondForm==null)
           { 
            SecondForm = new ShowNewForm();
            SecondForm.Show();
           }
           else if (SecondForm != null)
           {
               SecondForm.BringToFront();
           }
        }

Open in new window

0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33541738
Hi,

Thanks for your reply. Your code is working well. Finally i got a solution that what is my exact requirement.

Thanks
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33541760
Hi,

I would request you to cooperate me while allocating points (if you think my solution was helpful).
Because three users earlier replied me that they are not able to allocate points to my id. So please take time to allocate points till problem not solved out.

Thanks and Regards,
VSS
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33564867
Hi,

 I am not getting clearly about your comment.(ID: 33541760).

Thanks
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33564902
Hi  Rajkumar,

Actually there was a bug in alloting points to my account earlier. Now it has been solved, so now you can assign points.

Thanks and Regards,
VSS
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unlocking a column in excel using C# 17 28
ASP.NET - Why is accordion not working? 3 50
Making exceptions for ValidationAttribute 2 27
"The resource cannot be found" 1 24
If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question