473,757 Members | 10,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3792
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***@discussi ons.microsoft.c omwrote in message
news:09******** *************** ***********@mic rosoft.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***@discussi ons.microsoft.c omwrote in message
news:09******** *************** ***********@mic rosoft.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(My CallbackArgs args);
}

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

public void Run() {
while(true) {
DoALotOfWork();
listener.Notify Update(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
MulticastDelega te, 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(str ing s)
{
Console.WriteLi ne ("First: {0}", s);
}

static void SecondMethod(st ring s)
{
Console.WriteLi ne ("Second: {0}", s);
}
}

--
Jon Skeet - <sk***@pobox.co m>
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 MyHandyIntFunct ion(int p);

public MyHandyIntFunct ion myDelegate;

public event MyHandyIntFunct ion 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
MulticastDelega te, 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(str ing s)
{
Console.WriteLi ne ("First: {0}", s);
}

static void SecondMethod(st ring s)
{
Console.WriteLi ne ("Second: {0}", s);
}

}

--
Jon Skeet - <sk...@pobox.co m>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 MyHandyIntFunct ion(int p);

public MyHandyIntFunct ion myDelegate;

public event MyHandyIntFunct ion 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
24852
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 a function pointer, a pointer to user data (what is it?) and then I notify it when a certain event happens? I'm new to C, btw :-) Thanks a lot for helping.
3
1389
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 have arrived. At the same time the application uses the Timer object. The Tick-event of this timer is set to 50 seconds. The problem is when the callback function is triggered the timer tick-event
3
1121
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 get called multiple times and error out with the following Error: <EndInvoke can only be called once for each asynchronous operation.> If I comment out the RaiseEvent, then the Callback exits with a single call to EndDelegate.
0
1018
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 the calling class by using Addressof (if that makes any sense) I came up with: - Public Shared Function AddTaskItem(ByVal p_Label As String, ByVal p_Icon As Icon, ByRef callback As System.EventHandler) As Boolean Dim pTaskItem As New...
0
1313
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 window. Is this expected, and can I avoid this? To expose the problem, run this script and click the mouse button over the application window. When the file save dialog is through, the function "callback" is called, which I did not expect because...
11
1450
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 use the frame.txtobj to change displaying strings. I found it is a bad method to do that.
4
1836
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 Class {
8
5027
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 remote event. The following behavior can be observed: 1.) Right after the start of the server the first response via remote event will take a long time.
1
3017
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 properties of the event (ie. event type, message, etc.) from the OnObjectReady() callback. class SinkClass(object): def OnObjectReady(self, *args): #self may be the wmi_sink object print "OnObjectReady callback ..." print self...
0
9489
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.