• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1428
  • Last Modified:

Tray Notification Icon in windows service

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
Rajkumar Gs
Asked:
Rajkumar Gs
  • 10
  • 8
  • 4
  • +1
2 Solutions
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
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
 
Rajkumar GsSoftware EngineerAuthor Commented:
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
 
Rajkumar GsSoftware EngineerAuthor Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
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
 
kjetilroeCommented:

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
 
Rajkumar GsSoftware EngineerAuthor Commented:
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
 
joriszwaenepoelCommented:
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
 
kjetilroeCommented:

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
 
Rajkumar GsSoftware EngineerAuthor Commented:
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
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
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
 
kjetilroeCommented:

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

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

0
 
joriszwaenepoelCommented:
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
 
Rajkumar GsSoftware EngineerAuthor Commented:
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
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi,

May I know which link you used for reference?

Regards,
VSS
0
 
Rajkumar GsSoftware EngineerAuthor Commented:
Hi,

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

Thanks
0
 
Rajkumar GsSoftware EngineerAuthor Commented:
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
 
kjetilroeCommented:

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
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
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
 
Rajkumar GsSoftware EngineerAuthor Commented:
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
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
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
 
Rajkumar GsSoftware EngineerAuthor Commented:
Hi,

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

Thanks
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
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
 
Rajkumar GsSoftware EngineerAuthor Commented:
Hi,

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

Thanks
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 10
  • 8
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now