M
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!
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!