Question

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).

Was it helpful?

Solution

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

Here you can identify the exact issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top