Solved

SubscribeCallbacks / Event Handler

Posted on 2006-07-11
4
212 Views
Last Modified: 2012-05-05
Using the below code example...   I have a sample app where..  as the dataevents occur...   the dataevent's eventhandler basically calls an event click from a button control named BtnData..  in order to run the handling code which contains a for loop...     questions are below after the code sample...   i'm "spacing" here as to what's possible with using events outside of using them with controls (i.e.  buttons, key press, etc..)


public Button BtnData;

//
// BtnData
//
this.BtnData.Name = "BtnData";
this.BtnData.Text = "BtnData";
this.BtnData.Click += new System.EventHandler(this.BtnData_Click);


dataevent = data;
dataevent.GBEvent += new GBLib.IGBEventVtbl_GBEventHandler(dataevent_GBEvent);
data.SubscribeCallback("DataEvents", 1);

private void dataevent_GBEvent (object pISrc, string bstrData, string bstrHint, object pIEventData)
{
       BtnData_Click(BtnData, new EventArgs());
}

private void BtnData_Click (Object eventSender, EventArgs eventArgs)
{
     for (int i = 0; i <= collection.size - 1; i++)
     {
           < ... do some stuff here ...  >
     }
}

----------------------------------------------------------------------------------------

1)   What would the reason be as to why the sample app needs to call a control's event click this in order to fire the event?
2)   Is there a way to fire an event in a similar fashion without using a control button?  or any controls event ?
3)   What is the flow of the event args as they are passed from function to function?



0
Comment
Question by:lblinc
  • 2
4 Comments
 
LVL 19

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 100 total points
ID: 17087134
I suspect you need to use delegates to create the events you want.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkEventsTutorial.asp
0
 

Author Comment

by:lblinc
ID: 17087175

Actually, I just recalled the support for this API's dataevent_GBEvent  saying that the reason for handling in this manner is because the data is too large to disseminate directly to the handler in realtime quantities...   and that i t is up to the client to request the data through another event at needed intervals, or whenever necessary..   does that make sense?
0
 
LVL 19

Expert Comment

by:Daniel Van Der Werken
ID: 17087205
I've never heard of that size limitation.  It's possible.

I guess you answered question 1.

The primary thing you're interested in an event is the sender and eventargs.  Any appropriate control should work.

Event args are passed back via the callback.  When you click on a button, the specific details for that control defined by the program send back the current arguments.

0
 
LVL 13

Accepted Solution

by:
devsolns earned 400 total points
ID: 17087489
Here is an easy to understand example of how to create your own events and how to trigger them.


//create some custom event args.
public class AppleEventArgs : EventArgs
{
    public readonly string EatenBy;

    public AppleEventArgs(string eatenBy)
    {
        EatenBy = eatenBy;
    }
}

public class Apple
{
    //create event delegate(type safe function pointer)
    public delegate void AppleEatenDelegate(object sender, AppleEventArgs args);

    //create the event
    public event AppleEatenDelegate OnAppleEaten = null;

    //create the event invoker, should be protected if plan on not sealing class.
    protected void OnAppleEatenInvoke(string eatenBy)
    {
        //first line ensures its thread safe.
        AppleEatenDelegate appleEatenEvent = OnAppleEaten;
        if (appleEatenEvent != null) appleEatenEvent(this, new AppleEventArgs(eatenBy));
    }

    public void EatApple(string eatenBy)
    {
        //eat the apple here, now
        //trigger the event in this method.
        OnAppleEatenInvoke(eatenBy);
    }
}



and to use it,

Apple apple = new Apple();
apple.OnAppleEaten += new Apple.AppleEatenDelegate(apple_OnAppleEaten);
apple.EatApple();

void apple_OnAppleEaten(object sender, AppleEventArgs args)
{
    throw new Exception("The method or operation is not implemented.");
}


-gp
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now