Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

Event or Callback function?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MikeZ - 18 Jul 2007 20:10 GMT
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
Nicholas Paldino [.NET/C# MVP] - 18 Jul 2007 20:21 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>I have a thread that parser video-frame from network streaming. It can get
>up
[quoted text clipped - 6 lines]
>
> Thanks
AlexS - 18 Jul 2007 20:22 GMT
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?

>I have a thread that parser video-frame from network streaming. It can get
>up
[quoted text clipped - 6 lines]
>
> Thanks
MikeZ - 18 Jul 2007 20:44 GMT
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.

> Event = callback. Same function pointers. Event executes function
> synchronously, callback if you use it in Begin... function might be
[quoted text clipped - 18 lines]
> >
> > Thanks
Bram - 19 Jul 2007 17:55 GMT
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
Jon Skeet [C# MVP] - 19 Jul 2007 22:39 GMT
> 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);
   }
}

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Bram - 20 Jul 2007 11:13 GMT
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

> > Events are multicast and delegates are single-cast.
>
[quoted text clipped - 36 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
Jon Skeet [C# MVP] - 20 Jul 2007 14:45 GMT
> 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
[quoted text clipped - 5 lines]
>
> public event MyHandyIntFunction myEvent;

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

Jon

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.