C#: Why am I still getting an RCW error when exiting my application?
- by shifuimam
To be specific, the error is:
  An attempt has been made to free an RCW that is in use.  The RCW is in use on the active thread or another thread.  Attempting to free an in-use RCW can cause corruption or data loss.
The application in question is a small applet that displays the current clock speed of the CPU as a system tray icon. I'm using a System.Timers.Timer object to refresh the systray icon at an interval of 2.5 seconds. There's a single context menu item to exit the application, which uses the following function to unload everything:
    public void ExitApp(object sender, EventArgs e)
    {
        //stop and disable the timer
        theInterval.Stop();
        theInterval.Enabled = false;
        //unload the system tray icon
        TheIcon.Visible = false;
        //exit the application
        Application.Exit();
    }
If I take out everything but Application.Exit(); I get an RCW error and the systray icon doesn't go away until I mouse over it. Everything was working perfectly until I changed the window style of the hidden form used to initialize the systray icon and the timer.
If it helps any, I'm using C# 2010 Express.