Event or Callback function?

G

Guest

I have a thread that parser video-frame from network streaming. It can get up
to 1000 frame/second. I want another thread to process video-frame. I can use
RaiseEvent or Callback Function. My question is:
1) Which way is more efficiency? Event or Callback function.
2) Does <Event> run on Main Window thread? This is a windows application.
All form controls event run on main form thread.

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

You are going to get better performance generally from a function than
from a delegate. As for where it executes, the method/delegate will execute
in the thread that raised the event or made the call. In order to make the
call on the UI thread, you have to make a call to the Invoke method on a
control instantiated on the UI thread (any control, in most places). Since
the call to Invoke is going to require a delegate anyways, you probably will
end up with that method.
 
A

AlexS

Event = callback. Same function pointers. Event executes function
synchronously, callback if you use it in Begin... function might be
asynchronous. Both will run on the thread, which invokes them, so you will
need to check InvokeRequired and act accordingly.

It's not clear what is your architecture, how many threads you have and what
do you need to pass between them

You have UI thread where your form is running and you mentioned another
thread where you want to process frames. Which thread is receiving frames?
What will you do with frames after processing, show them on form or what?
 
G

Guest

Alex, I agree with you. I may just use Event. My parser thread is not an UI
thread. Processing Frames takes too much time, it will slow down the parser
thread speed. This is the reason that I want another thread handle extra work.
 
B

Bram

Events are multicast and delegates are single-cast. A delegate is
basically a variable containing a reference to one method, while one
event may have several subscribers.

I surmise that you only need one callback, so the event represents an
unneccesary overhead. Probably the delegate will be faster, although
not by much. An even faster method is by using an interface like this:

public interface IMyCallback {
void NotifyUpdate(MyCallbackArgs args);
}

public class MyClass {
IMyCallback listener;
public MyClass(IMyCallback listener) {
this.listener = listener;
}

public void Run() {
while(true) {
DoALotOfWork();
listener.NotifyUpdate(new MyCallbackArgs());
}
}
}

It has a distinct java feel to it but it will be a bit faster than
using a delegate. However, architecturally a delegate seems to make
more sense and the overhead isn't that large anyway so probably
delegates are the way to go.

Regards,
Bram
 
J

Jon Skeet [C# MVP]

Bram said:
Events are multicast and delegates are single-cast.

No, that's not true at all.

An event is fundamentally just a pair of methods - add and subscribe.
They've got to use delegates to do their job properly. Delegates are
inherently multicast. (At one stage MS was going to differentiate
between single-cast and multicast, hence Delegate and
MulticastDelegate, but they decided against it in the end.)

Here's a short but complete example demonstrating this - no events are
used anywhere, but it's clearly multicast behaviour.

using System;

class Test
{
static void Main()
{
Action<string> x = FirstMethod;
x += SecondMethod;

x ("Test");
}

static void FirstMethod(string s)
{
Console.WriteLine ("First: {0}", s);
}

static void SecondMethod(string s)
{
Console.WriteLine ("Second: {0}", s);
}
}
 
B

Bram

Hi Jon!

Mea culpa, thanks for correcting me. But now I'm not sure what the
difference between a delegate and an event is. What is the difference
between the delegate and the event in the following piece of code?

public delegate void MyHandyIntFunction(int p);

public MyHandyIntFunction myDelegate;

public event MyHandyIntFunction myEvent;

Both support the += and -= operators and both are multicast (contrary
to my previous belief).

Regards
Bram Fokke
 
J

Jon Skeet [C# MVP]

Mea culpa, thanks for correcting me. But now I'm not sure what the
difference between a delegate and an event is. What is the difference
between the delegate and the event in the following piece of code?

public delegate void MyHandyIntFunction(int p);

public MyHandyIntFunction myDelegate;

public event MyHandyIntFunction myEvent;

There are details at http://pobox.com/~skeet/csharp/events.html

Jon
 

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