470,647 Members | 1,306 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

Weird crash after "dragging" NotifyIcon - NullReferenceException

Me
I'm getting a NullReferenceException in Unknown Module when I follow
the below steps to create a simple NotifyIcon app that creates the
context menu on the fly(see a little analysis after the steps).

1. Create a new Windows Forms C# solution (I called mine DBChanger).
2. Replace Form1.cs code with this:

--------------------
using System;

namespace DBChanger
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
DBChangerIcon d = new DBChangerIcon();
d.Start();
System.Windows.Forms.Application.Run();
}
}
}
--------------------

3. Create a new class named "DBChangerIcon" and replace code with
this:

--------------------
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DBChanger
{
public class DBChangerIcon
{
NotifyIcon ni;

public void Start()
{
ni = new NotifyIcon();
ni.Icon = new Icon(@"..\..\App.ico");
ni.MouseDown += new MouseEventHandler(ni_MouseDown);

ni.Visible = true;
}

private void miExit_Click(object Sender, EventArgs e)
{
ni.Visible = false;
Application.Exit();
}

private void ni_MouseDown(object Sender, MouseEventArgs e)
{
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add(new MenuItem("E&xit", new
EventHandler(miExit_Click)));

ni.ContextMenu = cm;
}
}
}
--------------------

4. Build and run it.
5. Right click on the tray icon, drag the mouse off the tray icon,
then release the right click.
6. Right click on the tray icon again, release, and click Exit.

That's how I get a NullReferenceException. If I just do step 6
without step 5 then everything works fine.

It happens in the garbage collection during the Application.Exit()
call. Stack trace is:

System.GC.GetTotalMemory
System.Windows.Forms.Application.CollectAllGarbage
System.Windows.Forms.Application.Exit

And it always fails around the second time that function calls
GC.nativeGetTotalMemory();

This is with Framwork 1.1 and I made it in VS.Net 2003.

I can't figure out what I'm missing. Thanks for any help!
Nov 15 '05 #1
3 1945
Me
d0*******@sneakemail.com (Me) wrote in message news:<17**************************@posting.google. com>...
I'm getting a NullReferenceException in Unknown Module when I follow
the below steps to create a simple NotifyIcon app that creates the
context menu on the fly(see a little analysis after the steps).

1. Create a new Windows Forms C# solution (I called mine DBChanger).
2. Replace Form1.cs code with this:

--------------------
using System;

namespace DBChanger
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
DBChangerIcon d = new DBChangerIcon();
d.Start();
System.Windows.Forms.Application.Run();
}
}
}
--------------------

3. Create a new class named "DBChangerIcon" and replace code with
this:

--------------------
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DBChanger
{
public class DBChangerIcon
{
NotifyIcon ni;

public void Start()
{
ni = new NotifyIcon();
ni.Icon = new Icon(@"..\..\App.ico");
ni.MouseDown += new MouseEventHandler(ni_MouseDown);

ni.Visible = true;
}

private void miExit_Click(object Sender, EventArgs e)
{
ni.Visible = false;
Application.Exit();
}

private void ni_MouseDown(object Sender, MouseEventArgs e)
{
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add(new MenuItem("E&xit", new
EventHandler(miExit_Click)));

ni.ContextMenu = cm;
}
}
}
--------------------

4. Build and run it.
5. Right click on the tray icon, drag the mouse off the tray icon,
then release the right click.
6. Right click on the tray icon again, release, and click Exit.

That's how I get a NullReferenceException. If I just do step 6
without step 5 then everything works fine.

It happens in the garbage collection during the Application.Exit()
call. Stack trace is:

System.GC.GetTotalMemory
System.Windows.Forms.Application.CollectAllGarbage
System.Windows.Forms.Application.Exit

And it always fails around the second time that function calls
GC.nativeGetTotalMemory();

This is with Framwork 1.1 and I made it in VS.Net 2003.

I can't figure out what I'm missing. Thanks for any help!


Hmm... Anybody have a better way to create a context menu for a
NotifyIcon on the fly?

Anybody confirm that this happens or doesn't happen on their machine?
I'm using Win2000 Pro with SP3.

Thanks!
Nov 15 '05 #2

"Me" <d0*******@sneakemail.com> wrote in message
news:17**************************@posting.google.c om...
d0*******@sneakemail.com (Me) wrote in message

news:<17**************************@posting.google. com>...
4. Build and run it.
5. Right click on the tray icon, drag the mouse off the tray icon,
then release the right click.
6. Right click on the tray icon again, release, and click Exit.

That's how I get a NullReferenceException. If I just do step 6
without step 5 then everything works fine.

It happens in the garbage collection during the Application.Exit()
call. Stack trace is:

System.GC.GetTotalMemory
System.Windows.Forms.Application.CollectAllGarbage
System.Windows.Forms.Application.Exit

And it always fails around the second time that function calls
GC.nativeGetTotalMemory();

This is with Framwork 1.1 and I made it in VS.Net 2003.

Anybody confirm that this happens or doesn't happen on their machine?
I'm using Win2000 Pro with SP3.

Thanks!

Me (You),

I built this on WinXP SP1a with VS2003Pro, .Net 1.1.

No Problems with the Exception. However the "Exit" context menu stays up
afer the first right click and until you actually close the application.


Nov 15 '05 #3
Me
"TonyM" <Sp*******@Biteme.net> wrote in message news:<_x**********************@news.easynews.com>. ..
"Me" <d0*******@sneakemail.com> wrote in message
news:17**************************@posting.google.c om...
d0*******@sneakemail.com (Me) wrote in message

news:<17**************************@posting.google. com>...
4. Build and run it.
5. Right click on the tray icon, drag the mouse off the tray icon,
then release the right click.
6. Right click on the tray icon again, release, and click Exit.

That's how I get a NullReferenceException. If I just do step 6
without step 5 then everything works fine.

It happens in the garbage collection during the Application.Exit()
call. Stack trace is:

System.GC.GetTotalMemory
System.Windows.Forms.Application.CollectAllGarbage
System.Windows.Forms.Application.Exit

And it always fails around the second time that function calls
GC.nativeGetTotalMemory();

This is with Framwork 1.1 and I made it in VS.Net 2003.

Anybody confirm that this happens or doesn't happen on their machine?
I'm using Win2000 Pro with SP3.

Thanks!

Me (You),

I built this on WinXP SP1a with VS2003Pro, .Net 1.1.

No Problems with the Exception. However the "Exit" context menu stays up
afer the first right click and until you actually close the application.


Thanks for trying it!

I found an XP SP1 machine to test on. That behavior (menus staying
after drag) seems to happen with all tray icons in XP. Weird.

I can reproduce my bug by left clicking instead of right clicking in
step 5. So the last steps that make it fail in XP are:

5. Left click on the tray icon, drag the mouse off the tray icon, then
release the left click.
6. Right click on the tray icon, release, and click Exit.

It's looking like I'd better just not use a tray icon. ;) Shame
really.
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Miles Davenport | last post: by
10 posts views Thread by Flare | last post: by
1 post views Thread by Seraph | last post: by
1 post views Thread by Korara | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.