Solved

Tray Notification Icon in windows service

Posted on 2010-08-26
24
992 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
  • 10
  • 8
  • 4
  • +1
24 Comments
 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 23

Author Comment

by:Rajkumar Gs
Comment Utility
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
Comment Utility
Hi,

May I know which link you used for reference?

Regards,
VSS
0
 
LVL 23

Author Comment

by:Rajkumar Gs
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi,

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

Thanks
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now