C#: Why am I still getting an RCW error when exiting my application?

Posted by shifuimam on Stack Overflow See other posts from Stack Overflow or by shifuimam
Published on 2010-06-11T00:24:58Z Indexed on 2010/06/11 0:32 UTC
Read the original article Hit count: 377

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about timer