在 .NET 中使用 RaiseEvent 是否有任何性能开销?

发布于 2024-08-30 23:07:26 字数 891 浏览 3 评论 0原文

.NET 中使用 RaiseEvent 是否有任何性能开销?

我有一个类似于以下的代码。

Dim _startTick As Integer = Environment.TickCount
'Do some Task'
Dim duration As Integer = Environment.TickCount - _startTick
Logger.Debug("Time taken: {0}", duration)
RaiseEvent Datareceived()

上面的代码返回:

Time taken: 1200

Time taken: 1400

但是如果我删除 RaiseEvent 它会返回:

Time taken: 110

Time taken: 121

我很惊讶在记录所用时间之后调用 RaiseEvent 。它如何影响总花费时间?

我正在开发 .NET Compact Framework

在事件处理程序中,我给出了一个 MsgBox。当我删除消息框时,它现在显示的时间为 110、121 等,即少于 500 毫秒。如果我将 Msgbox 放回事件处理程序中,它会显示 1200、1400 等,即超过一秒。

我现在更惊讶了(该事件是在日志记录部分之后引发的)。

Is there any performance overhead in using RaiseEvent in .NET?

I have a code which is similar to following.

Dim _startTick As Integer = Environment.TickCount
'Do some Task'
Dim duration As Integer = Environment.TickCount - _startTick
Logger.Debug("Time taken: {0}", duration)
RaiseEvent Datareceived()

The above code returns:

Time taken: 1200

Time taken: 1400

But if I remove RaiseEvent it returns:

Time taken: 110

Time taken: 121

I am surprised that the RaiseEvent is called after the logging of time taken. How does it affect the total time taken?

I am working on .NET Compact Framework.

In the Eventhandler I had given a MsgBox. When I removed the message box, it is now showing time taken as 110, 121, etc., that is, less that 500 milliseconds. If I put the Msgbox back in eventhandler it shows 1200, 1400, etc., that is, more than a second.

I am more surprised now (the event is raised after the logging part).

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

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

发布评论

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

评论(1

来日方长 2024-09-06 23:07:26

尝试在控制台应用程序中使用相同的内容。控制台应用程序使用较少的资源。

在这里您可以确定确切的问题。

Try using the same in a console application. A console application uses fewer resources.

Here you can identify the exact issue.

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