MarkJoel60
MarkJoel60

Reputation: 557

Specified cast is not valid error at runtime, but not in debugger

I've looked through the different questions, but none of them seem to apply. This is VS2010 running on a Windows 7 64Bit system.

I was recently adding code to a form that has been running fine for several months. I made some changes to the activate event. Suddenly, it started throwing an exception at runtime. Not in the debugger. If I set the JIT runtime debugger (i.e. system.windows.forms jitDebugging="true"), the error also goes away (on my development machine, I'm guessing because it has VS2010 installed -- but not on the client's machine)

Of course, I backed out my code (uncommenting it) but the error message stays. Because the debugger doesn't throw any exception, I don't know where the problem is.

I have tried turning on Common Language Runtime Exceptions, but it still doesn't see any errors.

I put debug lines in and sent it to a file. The Activate event is called, but the Shown event does not seem to be called before the exception is thrown. I don't have any methods defined for anything except Shown now... but my debug write does not get put in the file before the exception is thrown, so if the problem is in that method, I have no idea where.

The full error that is shown is:

System.InvalidCastException: Specified cast is not valid.
   at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)
   at System.Windows.Forms.StringSource..ctor(String[] strings)
   at System.Windows.Forms.TextBox.SetAutoComplete(Boolean reset)
   at System.Windows.Forms.TextBox.OnHandleCreated(EventArgs e)
   at System.Windows.Forms.Control.WmCreate(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
   at System.Windows.Forms.TextBox.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

[EDIT] The Shown Method:

int shownHeight = -1;
int shownTop = -1;
int ButtonOneTop = -1;
int MagnifiedPanelTop = -1;
int MagnifiedPanelHeight = -1;
int SelectedStateTop = -1;

private void VoiceCommandInput_Shown(object sender, EventArgs e)
{
    _ThisForm = this;
    VoiceTextbox.CharacterCasing = CharacterCasing.Upper; ;
    voiceCommandButton.Focus();
    _ThisForm.Height = MagnifiedPanel.Height + 38;

    keyboardPanel.Top = MagnifiedPanel.Bottom + 2;
    int chkptone = button1.Top;

    shownHeight = _ThisForm.Height;
    _ThisForm.Height = shownHeight + AdvancedButtonsPanel.Height + 10;
    shownTop = _ThisForm.Top;
    ButtonOneTop = chkptone;
    MagnifiedPanelTop = MagnifiedPanel.Top;
    MagnifiedPanelHeight = MagnifiedPanel.Height;
    SelectedStateTop = selectedState.Top;
    AdvancedButtonsPanel.Top = selectedState.Bottom + 20;
    AdvancedButtonsPanel.Visible = true;
    AdvancedButtonsPanel.BringToFront();
    keyboardPanel.Top = AdvancedButtonsPanel.Bottom + 20;

    BKD_buttonPusher.WorkerReportsProgress = true;
    BKD_buttonPusher.WorkerSupportsCancellation = true;
    BKD_buttonPusher.DoWork += new DoWorkEventHandler(this.buttonPusher_DoWork);
    BKD_buttonPusher.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.buttonPusher_RunWorkerCompleted);


    VoiceTextbox.Focus();
}

Upvotes: 3

Views: 2547

Answers (1)

bluish
bluish

Reputation: 27400

I paste here @hvd's comment, because it could help someone

A quick Google search brings up this, and if you did anything like what's described there (removed [STAThread] from Main, or set up UI controls from background threads), I can imagine that it would fail, and I can also imagine that seemingly irrelevant changes such as attaching a debugger might set up the thread subtly differently, making it not throw exceptions.

Upvotes: 3

Related Questions