指定的强制转换在运行时无效,但在调试器中无效

时间:2014-10-10 04:34:16

标签: c# .net winforms visual-studio-2010

我已经查看了不同的问题,但似乎都没有适用。这是VS2010在Windows 7 64Bit系统上运行。

我最近在一个运行良好数月的表单中添加了代码。我对activate事件做了一些更改。突然,它开始在运行时抛出一个异常。不在调试器中。如果我设置了JIT运行时调试器(即system.windows.forms jitDebugging =“true”),错误也会消失(在我的开发机器上,我猜是因为它安装了VS2010 - 但不是在客户端的机器上)

当然,我退出了我的代码(取消注释)但错误消息仍然存在。因为调试器没有抛出任何异常,所以我不知道问题出在哪里。

我尝试启用Common Language Runtime Exceptions,但它仍然没有看到任何错误。

我将调试行放入并将其发送到文件。调用Activate事件,但在抛出异常之前似乎没有调用Shown事件。我没有为除了现在显示的任何内容定义任何方法...但是在抛出异常之前我的调试写入没有被放入文件中,所以如果问题出在那个方法中,我不知道在哪里。

显示的完整错误是:

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)

[编辑] 显示方法:

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();
}

1 个答案:

答案 0 :(得分:2)

我粘贴@ hvd的评论,因为它可以帮助某人

快速Google搜索会显示this,如果您执行的操作与此处描述的内容(从Main中删除[STAThread],或从后台线程设置UI控件),我可以想象它会失败了,我也可以想象看似无关紧要的变化,如附加调试器,可能会巧妙地设置线程,使其不会抛出异常。