473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Event or Callback function?

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 <Eventrun on Main Window thread? This is a windows application.
All form controls event run on main form thread.

Thanks
Jul 18 '07 #1
6 3744
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?

"MikeZ" <Mi***@discussions.microsoft.comwrote in message
news:09**********************************@microsof t.com...
>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 <Eventrun on Main Window thread? This is a windows application.
All form controls event run on main form thread.

Thanks

Jul 18 '07 #2
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.

"AlexS" wrote:
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?

"MikeZ" <Mi***@discussions.microsoft.comwrote in message
news:09**********************************@microsof t.com...
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 <Eventrun on Main Window thread? This is a windows application.
All form controls event run on main form thread.

Thanks


Jul 18 '07 #3
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
Jul 19 '07 #4
Bram <b.*****@gmail.comwrote:
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<stringx = 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);
}
}

--
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
Jul 19 '07 #5
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
Bram <b.fo...@gmail.comwrote:
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<stringx = 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);
}

}

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

Jul 20 '07 #6
On Jul 20, 11:13 am, Bram <b.fo...@gmail.comwrote:
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

Jul 20 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Phui Hock | last post by:
Hi, How do I generate an event and got it handled by a handler? I know how to do it in C++ or Java but I got no idea how to do it in C. What is the best approach to write a function that will take...
3
by: Marten Van Keer | last post by:
Hello; I have an application that waits for TCP packets to arrive on a network stream. A callback function(using an asynccallback delegate) is implemented to tell the application some packets...
3
by: Jon Turner | last post by:
I have an asynchronous call to a remote procedure that is invoked thru BeginInvoke. The problem is that in the Delegate if I throw an event before the CallBack function exits, the CallBack will...
0
by: Paul Trippett | last post by:
Hi, What I am looking to do is call a function which will generate an object based on its passed parameters, add it to another control and when the generated item is clicked pass the event back to...
0
by: John Hunter | last post by:
The following behavior surprised me. I have a Tk window and launch a file save dialog from it. When the filesave dialog is finished, it calls callbacks bound to the destroy event on the main...
11
by: Jia Lu | last post by:
HI all I am making an application with wxpython. But I got a problem when I want to change the display string according to process status. I passed the frame to the processing function and...
4
by: jwriteclub | last post by:
Hello all, I have a quick question about timers in C#. I have a System.Windows.Forms.Timer timer (the kind you "drag" out of the toolbox. It looks something like this: public class...
8
by: schaf | last post by:
Hi NG! I have a problem in my remote application. After calling a remote function the calculation will be done by the service. The calculation result will be sent to the caller (client) via...
1
by: hainguyen2x | last post by:
I'm trying to get a notification from the NT event for any new event using the DispatchWithEvents() function. Everything seems to be working the way I wanted, but I don't know how to get the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.