Too slow responding to an event

G

Guest

Hi, I have a problem where I use an event to signal another part of my
program to process a message. The idea is something like this

Class1 handles the first part of the processing for a message received from
the network. When I reach a certain point in the processing an event is
raised and Class2 is supposed to continue processing the message. The message
is passed as an argument through the delegate and it works apart from one
thing; timing.

Sometimes Class2 doesn't its processing untl 100 ms after the event is raised.

In Class1 I have the following code:

public event MyEventHandler Notify;

public delegate void MyEventHandler(MyMessageClass message, Class1 sender);

public void ProcessMessage(MyMessageClass messageObject)
{
//Do the first part of processing on messageObject

// Notify Class2 to take over
Notify(messageObject, this);
}

In Class2 I have the following code:

private void ProcessMessage_Notify(MyMessageClass message, Class1 sender)

I also subscribe to the event using the code
Class1.Notify += new MyEventHandler(ProcessMessage_Notify);


Why is there a delay when signaling like this?

Is there a better way to get the same functionaliy to avoid this delay?
I need to separate the processing in different classes since they not belong
to the same dll in my project and I need to keep them separated.

Thank you,

Henrik
 
B

Barry Kelly

Henrik said:
Class1 handles the first part of the processing for a message received from
the network. When I reach a certain point in the processing an event is
raised and Class2 is supposed to continue processing the message. The message
is passed as an argument through the delegate and it works apart from one
thing; timing.

Sometimes Class2 doesn't its processing untl 100 ms after the event is raised.

What are you using to measure? I have two files, TestLib.cs compiled
with "csc /t:library TestLib.cs" and Test.cs, compiled with "csc
/r:TestLib.dll Test.cs". Here's TestLib.cs:

---8<---
using System;
using System.Diagnostics;

public static class HandlerClass
{
public static void Handle(Stopwatch watch)
{
Console.WriteLine("{0:f7} seconds",
watch.ElapsedTicks / (double) Stopwatch.Frequency);
}
}
--->8---

Here's Test.cs:

---8<---
using System;
using System.Diagnostics;

static class App
{
static Action<Stopwatch> MyEvent;

static void Main()
{
MyEvent += HandlerClass.Handle;
for (int i = 0; i < 10; ++i)
MyEvent(Stopwatch.StartNew());
}
}
--->8---

Here's the output:

---8<---
0.0003366 seconds
0.0000047 seconds
0.0000011 seconds
0.0000008 seconds
0.0000008 seconds
0.0000008 seconds
0.0000008 seconds
0.0000008 seconds
0.0000008 seconds
0.0000008 seconds
--->8---

The first couple of runs are slow because of JIT / cache warmup etc.
overhead. After that, it gets reasonably fast - on the order of 1
microsecond. That's not wonderfully fast for an Athlon 3500+ (the
machine I'm running on machine), but it's not remotely close to 100
milliseconds.

-- Barry
 
G

Guest

I'm using DateTime.Now to show when the event is signaled and when the
EventHandler starts executing.
 
B

Barry Kelly

Henrik said:
I'm using DateTime.Now to show when the event is signaled and when the
EventHandler starts executing.

The resolution of DateTime.Now is about 16 to 17 milliseconds. Don't
rely on it for microbenchmarks unless you've got iterations that take
many seconds, and you're performing a divide to get average time.

Can you provide sample code that demonstrates this bad performance? I.e.
the far worse scenario than the one I posted?

-- Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top