性能计数器注册表验证

发布于 2024-08-26 07:41:36 字数 333 浏览 5 评论 0原文

我有一个 C# 应用程序,它在启动时添加一些性能计数器。但如果注册表 HKEY_LOCAL_MACHINE->SOFTWARE->Microsoft->Windows NT->CurrentVersion->Perflib 已损坏(数据丢失或无效),则检查性能计数器是否存在的操作 (PerformanceCounterCategory.Exists(类别)在最终抛出异常(InvalidOperation:类别不存在)之前需要很长时间(大约 30 秒)。 我的问题是,在尝试添加性能计数器(以及有效性意味着什么)之前,如何验证注册表的有效性,或者是否有办法可以使性能计数器操作超时,这样就不需要 30 秒来获得一个例外。

I have a C# application that adds some performance counters when it starts up. But if the registry HKEY_LOCAL_MACHINE->SOFTWARE->Microsoft->Windows NT->CurrentVersion->Perflib is corrupted (missing or invalid data), the operation of checking the existence of the performance counters (PerformanceCounterCategory.Exists(category) takes a really long time (around 30 secs) before finally throwing exception (InvalidOperation: Category does not exist).

My question is how can i verify the validity of the registry before trying to add the performance counters (and what validity means) or if there is a way i can timeout the perf counter operations, so that it doesn't take 30 seconds to get an exception.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光的暖冬 2024-09-02 07:41:36

我怀疑“有效性”是您不了解的内部实现细节。但是,您至少可以尝试打开相同的注册表项并查看它们是否存在。您可以使用 Process Explorer 来确定它正在读取哪些键。

但是,我怀疑你为什么会关心。注册表损坏不应该是一件很常见的事情,如果是的话,您将采取什么措施?你所能做的就是放弃。所以你不妨捕获异常。我会像对待任何其他阻塞操作一样对待此操作,并在工作线程(而不是 UI 线程)上执行此操作,并向用户显示进度,以便他们知道您的应用程序没有挂起。

I suspect that "validity" is an internal implementation detail that you don't get to know. However, you could at least try to open the same registry keys and just see if they exist. You can use Process Explorer to figure out which keys it is reading.

However, I question why you would even care. A corrupted registry shouldn't be a very common thing, and if it is, what are you going to do about it? All you can do is quit. So you might as well just catch the exception. I would treat this like any other blocking operation and do it on a worker thread (not the UI thread) and show progress to your users so they know your app isn't hung.

晨光如昨 2024-09-02 07:41:36

您确定注册表已损坏吗?

我发现我的程序没有在运行时创建性能计数器的权限。相反,我将计数器的创建添加到我的安装程序中,并且安装程序必须以管理员身份运行。在运行时,我的程序在访问或更新已创建的计数器时没有问题。

Are you sure the registry is corrupt?

I found that my program did not have permission to create performance counters at run time. Instead, I added the creation of the counters to my installer program, and the installer must be run as an administrator. At run time my program had no problems accessing or updating the already created counters.

浪荡不羁 2024-09-02 07:41:36

我不知道如何回答您提出的问题,但如果我理解正确的话,问题是在用户看来,应用程序在启动时挂起长达 30 秒。

如果是这种情况,我建议您只需创建一个工作线程,告诉它创建性能监视器,然后继续启动应用程序,就可以避免这种情况。
我没有使用过性能计数器,所以我不能说它们是否有任何东西会阻止它工作?

I don't know how to answer your question as asked, but if I understand correctly, the problem is that it looks to the user as if the application hangs for up to 30 seconds on startup.

If that is the case, I'd suggest that you might be able to avoid that by just creating a worker thread, telling it to create the performance monitors and then carrying on with starting the application.
I've not worked with performance counters so I can't say if there's anything with them that would stop this from working?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文