Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SubscribeCallbacks / Event Handler

Posted on 2006-07-11
4
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 20

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 400 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 20

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

636 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