【问题标题】:Specified cast is not valid error at runtime, but not in debuggerSpecified cast is not valid error 在运行时,但不是在调试器中
【发布时间】:2014-10-10 04:34:16
【问题描述】:

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

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

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

我尝试打开公共语言运行时异常,但仍然没有发现任何错误。

我放入调试行并将其发送到文件中。调用了 Activate 事件,但在引发异常之前似乎没有调用 Shown 事件。除了 Shown now 之外,我没有为任何东西定义任何方法......但是在引发异常之前我的调试写入没有放入文件中,所以如果问题出在该方法中,我不知道在哪里。

显示的完整错误是:

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

【问题讨论】:

  • 也分享代码
  • 我不确定要分享什么代码...?我不确定问题出在哪里。完整的表格?完整的显示方法?我应该分享什么?
  • UnsafeNativeMethods.CoCreateInstance 表明它与非 C# 代码有关。正在调用一些 DLL/COM 组件/非 .net 代码。复制代码,注释掉所有不产生异常的代码,缩小问题范围。
  • bjan: “所有非异常生成代码”是一个挑战,因为我不知道异常是在哪里生成的。 (如果调试器实际上会抛出异常,那肯定会很好......)我最初添加的代码有 COM 代码——各种 DLL 导入,因为无论发生什么其他事情,我都将表单强制到顶部。那是事情变得糟糕的时候。但是我取消了所有这些,问题仍然存在。
  • 1- 保留注释行,除非客户端没有异常 2- 您可能已经更改了项目的属性(添加了一些引用,更改了一些配置等),因此创建新项目并继续将行从旧到新复制除非抛出异常。

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


【解决方案1】:

我把@hvd 的评论贴在这里,因为它可以帮助别人

快速的 Google 搜索会出现 this,如果您执行了与此处描述的操作类似的操作(从 Main 中删除了 [STAThread],或从后台线程设置 UI 控件),我可以想象它会失败,我还可以想象,看似无关的更改(例如附加调试器)可能会以微妙的方式设置线程,使其不会引发异常。

【讨论】:

  • 对我来说是“从后台线程设置 UI 控件”。我删除了该代码,错误就消失了。
猜你喜欢
  • 1970-01-01
  • 2020-06-05
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 2020-05-26
  • 1970-01-01
相关资源
最近更新 更多