- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
线程中的未处理异常进入 CLR来自非托管代码不会触发“正常”未处理的异常 CLR 处理。
在下面的代码中,使用 C++ 调用 CSSimpleObject.GetstringLength()
如果是“1”
情况“2”(预期行为)
必须做什么才能获得“正常”行为?
以下代码基于 Visual Studio 2010“CppHostCLR”代码示例来自“all interop and fusion samples”。
运行时主机(C++):
PCWSTR pszStaticMethodName = L"GetStringLength";
PCWSTR pszStringArg = L"1";
//PCWSTR pszStringArg = L"2";
hr = pClrRuntimeHost->ExecuteInDefaultAppDomain(pszAssemblyPath,
pszClassName, pszStaticMethodName, pszStringArg, &dwLengthRet);
if (FAILED(hr))
{
wprintf(L"Failed to call GetStringLength w/hr 0x%08lx\n", hr);
goto Cleanup;
}
托管代码 (C#):
public class CSSimpleObject
{
public CSSimpleObject()
{
}
//------8<----------
public static int GetStringLength(string str)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
switch (str)
{
case "1":
throw new Exception("exception in non-CLR-created thread");
case "2":
Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
thread.Start();
break;
}
return str.Length;
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine("CurrentDomain_UnhandledException:" + e.ToString());
Console.ReadKey();
}
public static void WorkThreadFunction()
{
throw new Exception(""exception in CLR-created thread"");
}
MSDN 最初暗示非 CLR 创建的线程中未处理的异常应该或多或少“自然地”表现 - 请参阅“Exceptions in Managed Threads”
The common language runtime allows most unhandled exceptions in threads to proceed naturally. In most cases this means that the unhandled exception causes the application to terminate."
“大多数”意味着在 CLR 创建的内部线程中,线程中止和应用程序域卸载异常的处理方式不同。在非 CLR 线程中
"they proceed normally, resulting in termination of the application."
进一步的研究使我找到了“Unhandled Exception Processing In The CLR”,在那里我发现了以下内容:
"if the exception was not handled ... in the managed method, the exception would exit the CLR but continue to propagate up the stack as a native SEH exception (managed exceptions are represented as native SEH exceptions) ... The OS unhandled exception filter (UEF) mechanism may not always result in triggering the CLR's unhandled exception processing. Under normal circumstances, this will work as expected and the CLR's unhandled exception processing will be triggered. However, in certain instances this may not happen."
上面的代码有什么问题或者怎么改才能触发CLR的未处理异常处理?
我刚刚发现了一个旧的错误报告,“当从非托管调用托管代码并引发异常时,未调用 UnhandledExceptionEventHandler - http://tinyurl.com/44j6gvu”,Microsoft 确认这是一个“错误”行为:
Thank you for taking the time to report this problem. The behavior is indeed a bug caused by the CLR execution engine and the CRT competing for the UnhandledExceptionFilter. The architecture of the CLR has been revised in the 4.0 version supporting this scenario.
为什么正确地做到这一点很重要?
注意: 通过 SetActionOnFailure()
更改 CLR 行为会使事情变得更糟,因为它最终会掩盖原始异常(即,而不是 out of内存你最终看到 threadAborts - 不知道原始错误凸轮来自哪里)!
最佳答案
更新意味着您可能在这里找到了解决方案。但是,它的解决方案并非在所有情况下都有效,因此这里有更多信息。
如果您更喜欢 CLR 未处理异常行为,则将其设为最外层程序并仅调用 native 代码以执行特定功能。这将确保 CLR 控制未处理的异常过滤器。
如果你想保留你当前的结构并且你的 C++ 代码很小,你可以完全停止使用 CRT(这会让你失去一堆有用的东西,包括静态构造函数和 C++ 异常处理)。这将确保 CLR 获得未处理的异常过滤器。
当然,您可以简单地自己调用 SetUnhandledExceptionFilter 并获得您想要的行为。
但是,我认为在这种情况下,最好的建议是在任何线程的调用堆栈上放置一个带有 catch block 的实际函数,以便在发生异常时执行某些操作,而不是依赖 UEF 机制——因为在组件系统的上下文中,由于多个用户竞争它,它总是很脆弱。
马丁
关于.net - CLR 在非 CLR 创建的线程中托管异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6124631/
我会尝试尽可能详细地描述我的问题,但如果需要更详细的解释,请告诉我。 为了简化,假设我有 3 个 DLL(实际上我有更多,但我猜这不是很重要): managed-1.dll - 托管 DLL(用 C#
我有一个 Tomcat 6 JSF Web 应用程序,我想使用 CDI bean 来设置它。不过,我必须逐渐将项目转换为 CDI。 我的问题是:CDI bean 和传统的 JSF 托管 bean 可以
我计划将 Web 应用程序从使用 JSF 托管 Bean 转换为使用 CDI 托管 Bean。我知道我需要执行以下操作: 在 WEB-INF 中添加一个空 beans.xml 文件。 将所有 JSF
我在裸域上运行我的解析应用程序。 Parse 并没有让我的生活变得轻松。 起初我很难设置它,因为大多数 DNS 托管服务不允许根域上的 CNAME,而 Parse 需要 CNAME。 决定尝试使用 C
如果您使用 Firebase 托管 将请求定向到 云函数 通过重写,“通过”托管的请求流量是否会记入 Firebase 托管 国标转出 ? (忽略 Cloud Function 的计费) 换句话说,
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
我有一个特定的用例,我正尝试使用 Firebase 托管来解决,它如下: 我正在使用 Gridsome 生成静态网站 在构建之前,Gridsome 使用 Graphql 并从 Headless CMS
您好,我有一个blazor页面,我想在其中显示一个变量。 这个变量从另一个线程(通过Websocket接收数据的另一个线程)获取更新,我想以线程安全的方式显示它: Blazor页面 @page "/n
我想使用 Firebase 托管来托管一个 angular 应用程序,我需要创建一个重定向到另一个 URL 中的一些旧文件。 根据 Firebase 文档,您可以进行基本重定向 "redirects"
我正在尝试找出满足电子学习平台以下要求的最佳方法。 我在其中编辑视频的后端,以便可以将它们彼此链接(例如youtube-comment函数) 用户只能在访问平台时观看视频(不允许下载!) 视频只能通过
已关闭。这个问题是 not about programming or software development 。目前不接受答案。 这个问题似乎不是关于 a specific programming
我想这是一个相当深入的主题,因此任何带有洞察信息的网址也很乐意接受。我一直在使用原生 DirectX,但从未管理过。另一方面,大多数情况下,在开发不需要高级 GPU 渲染的其他类型的应用程序时,我通常
我刚刚将一个网站部署到 Firebase 托管,效果非常好 - 设置非常简单。 但是,我的问题是,有什么方法可以使访问该网站受到身份验证的限制吗?这是一个管理面板,只有我的团队才能访问。有什么方法可以
已关闭。这个问题是 not about programming or software development 。目前不接受答案。 这个问题似乎不是关于 a specific programming
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
我刚刚将一个网站部署到 Firebase 托管,它运行良好 - 设置非常简单。 但是,我的问题是,有什么方法可以通过身份验证限制对网站的访问?这是一个只有我的团队才能访问的管理面板。有什么方法可以用密
如果我想托管一个对公众用处不大的应用程序(例如,一个将点打印到控制台的程序,它们的数量取决于现在的时间),我喜欢在启动板中的方式不必乱扔全局托管站点的命名空间并用诸如 HourDot 之类的名称填充它
我是一名优秀的程序员,十分优秀!