我得到这个异常:
Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException was unhandled HResult=-2146232832 Message=An exception 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException' occurred Source=Microsoft.VisualStudio.Debugger.Runtime StackTrace: at Microsoft.VisualStudio.Debugger.Runtime.Main.ThrowCrossThreadMessageException(String formatString) InnerException:
当我使用这段代码时:
public string ShowOpenFileDialog(string initialPath, string filter = null)
{
var dialog = new OpenFileDialog()
{
InitialDirectory = Directory.Exists(initialPath) ? Path.GetDirectoryName(initialPath) : String.Empty,
FileName = File.Exists(initialPath) ? Path.GetFileName(initialPath) : String.Empty,
Filter = filter
};
if (dialog.ShowDialog() == true)
{
return dialog.FileName;
}
return String.Empty;
}
我没有明确使用多线程,那么是什么原因造成的呢?它在调试期间经常发生,但不是一直发生。我已经将方法签名更改了大约一百万次,因为我正在传递 ref/out 参数并认为它们可能是原因。
编辑:我从 UI 线程调用它。 View 中的按钮 -> ViewModel 中的命令 -> ShowOpenFileDialog。
请您参考如下方法:
虽然我仍然不确定为什么会发生这种情况,但我已经想出如何解决我的问题。我的解决方案中有多个项目,并且配置了两个启动项目(客户端+服务器)。当我通过调试启动两个项目时,调试器似乎有问题。所以我只是关闭了目前最不重要的项目的调试,因此调试器只处理一个项目。
