Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

Here iam facing the Typical problem with notify icon.Here i have Developed a windows application that is Modem Device Detector.Main theme of the Applicaion is when i put any Modem to my CPU.Notify icon message will come, Modem connected sucessfully.When i remove the Modem,Modem disconnected Message will come through Notify icon Up to now this is fine.But When i Disconnected the Modem the Connected Notify icon is still in Active.So i dont know how to Remove the Instance of the Notify Icon

Sample Code: This is when Modem Connected event

C#
NotifyIcon notifyIcon2 = new NotifyIcon();
notifyIcon2.Icon = SystemIcons.Exclamation;
notifyIcon2.BalloonTipTitle = "SmsService Running...";
notifyIcon2.BalloonTipText = "Gsm Modem connected Successfully";
notifyIcon2.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon2.Icon = new Icon("Running.ico");
                          
notifyIcon2.Visible = true;
notifyIcon2.ShowBalloonTip(1);
notifyIcon2.Text = "Modem Connected" + e.Drive;


This is When Modem Disconnected event

C#
NotifyIcon notifyIcon3 = new NotifyIcon();
notifyIcon3.Icon = SystemIcons.Exclamation;
notifyIcon3.BalloonTipTitle = "SmsService Stopped..";
notifyIcon3.BalloonTipText = "Gsm Modem Disconnected Successfully";
notifyIcon3.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon3.Icon = new Icon("Stopped.ico");
notifyIcon3.Visible = true;
notifyIcon3.ShowBalloonTip(1);
notifyIcon3.Text = "Modem DisConnected" + e.Drive;
scrTemp.Stop();
scrTemp.Refresh();
notifyIcon3.Dispose();
notifyicon2.Dispose();

Even the notifyicon is in ACTIVE Still i have used Dispose Concept.


please share any information Regarding this.


Regards,

AnilKumar.D
Posted
Updated 10-Jul-12 18:32pm
v2

I think before you dispose of it you might try setting visibility to false. If you can move the mouse cursor over the icon and then it disappears that is what you will need to do. I have never tried disposing of a notify icon while program stays running so not sure how it reacts.

You actually should be able to re-use a single notify icon without continually disposing and initializing. I am sure you can just change the icon, text, and reconfigure the balloon tip when you need to change modes. I am positive that is how I have done something similar in past to show when scanner was connected/disconnected.

Edit:

Just found my sample from the scanner notify icon I mentioned so thought I'd share my example.

VB
Select Case EventID
  Case WIA_DEVICE_EVENTID.WIA_CONNECT

    Me.NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
    Me.NotifyIcon1.BalloonTipText = "Connected: " & vbCrLf & devsConnected & vbCrLf & "Disconnected:"
    Me.NotifyIcon1.ShowBalloonTip(2000)
    Me.NotifyIcon1.Icon = New Icon(...GetManifestResourceStream("Active.ico"), 16, 16)

  Case WIA_DEVICE_EVENTID.WIA_DISCONNECT
    Me.NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
    Me.NotifyIcon1.BalloonTipText = "Connected: " & vbCrLf & devsConnected & vbCrLf & "Disconnected:" & vbCrLf & ScannerDetails.GetScannerName(DeviceID.Split("\")(1))
    Me.NotifyIcon1.ShowBalloonTip(2000)

    If devsConnected = "" Then
      Me.NotifyIcon1.Icon = New Icon(...GetManifestResourceStream("InActive.ico"), 16, 16)
    Else
      Me.NotifyIcon1.Icon = New Icon(...GetManifestResourceStream("Active.ico"), 16, 16)
    End If

End Select
 
Share this answer
 
v2
Comments
Anil Honey 206 11-Jul-12 2:17am    
I understand your Concept Actually if you see any Pendrive connected to Cpu Pendrive is Connected Message will Come and while Disconnectec its showing Pendrive disconnected message will like that concept
Anil Honey 206 11-Jul-12 2:21am    
One more this My Application will not end and the user will not stop its Continue process when modem Disconnected also the application will run.Because when use wants to connect just he will connect the modem to cpu at that time Modem Connected message should come.
Trak4Net 11-Jul-12 3:01am    
I think I might be confused now. Do you want two or more icons showing at a time based on how many modems are connected or do you want a single notify icon to change and reflect when the event of connecting/disconnecting occurs and show the balloon tip stating what event occured?
Anil Honey 206 11-Jul-12 5:07am    
Only one Modem will Connect and Disconnect as per Events and one icon iam maintaining till now.But when disconnecting two icons are coming.
Trak4Net 11-Jul-12 10:40am    
Is this a windows forms application? Usually when I am writing an application that will not have any significant UI I set the startup form with the following properties: WindowsState == Minimized, ShowinTaskBar == false
I add a notify icon to the startup form. My processing class will be initialized from the form load and have events that the form can attach to and use to determine the state of the notify icon. Then I will adjust the icon, text, show a balloon tip based on the event. I never dispose of or initialize a new notify icon, always using the same one throughout the life of the program. I typically assign a context menu to the notify icon to handle exiting the program or showing a configuration UI based on the application.
This is actually a known problem. There are lots of suggestions on the internet.
Disposing it, as you already mention.
Setting the NotifyIcons Visible Property to false.
Setting the Icon Property to null.

Here's some stuff that may help too.
NotifyIcon still visible after closing form[^]
How to guarantee a NotifyIcon disappears?[^]
Issue with NotifyIcon not disappreading on Winforms App[^]
NotifyIcon will not disappear[^]

It may not be pretty. After all, it's a Component, it should do this stuff by default!... It doesn't though, and we'll have to live with it.
 
Share this answer
 
v2
Comments
Anil Honey 206 11-Jul-12 2:27am    
I want to hold the Notify icon because my application will not close or end or exist its like service

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900