# Events Driven Library XNA C#

Language: C# w/ XNA Framework

Relevant and hopefully helpful background info:

I am making a library using the XNA framework for games I make with XNA. The Library has a folder(Namespace) dedication to the GUI. The GUI Controls inherit a base class hooked with the appropriate Interfaces. After a control is made, the programmer can hook the control with a "Frame" or "Module" that will tell the controls when to update and draw with an event. To make a "Frame" or "Module", you would inherit a class with the details coded in. (Kind of how win forms does it.) My reason for doing this is to simplify the process of creating menus with intractable controls. The only way I could think of for making the events for all the controls to function without being class specific would be to typecast a control to an object and typecast it back. (As I have read, this can be terribly inefficient.)

Problem:

Unfortunately, after I have implemented interfaces into the base classes and changed

public delegate void ClickedHandler(BaseControl cntrl);


to

public delegate void ClickedHandler(Object cntrl, EventArgs e);


my game has decreased in performance. This performance could be how I am firing the events, as what happens is the one menu will start fine, but then slowly but surely will freeze up. Every other frame works just fine, I just think it has something to do with the events and... that is why I am asking about them.

Question:

Is there a better more industry way of dealing with GUI Libraries other than using and implementing Events?

Goal:

To create a reusable feature rich XNA Control Library implementing performance enhancing standards and so on.

• Could you try profiling your code to see where the performance bottlenecks are? Also, you could take a look at Nuclex framework source to see how they do it, maybe you could pick up some good practices. Nov 21 '12 at 21:54
• Ahh, that's neat. I will surely look into it. Thanks! Nov 23 '12 at 4:13
• I also did some profiling and found the issue. Thanks! Nov 23 '12 at 4:20

The lag was caused due to an update method in my base text control. I had a mistake of running a loop a ton of times when it did not need to be ran.

I have also remove all instances of EventArgs from the library as I personally see no point in them.

At this time, I have read quite a bit of posts saying events are appropriate for menus, but not necessarily for gameplay. The preferred way (That I have noticed.) is to use a state based form. So, really, you would check manually to see if something checks.

Wild stab in the dark:

You might have some events that fire very often (like every frame), for example OnMouseMove, and after your change of parameters you now create a new EventArgs at every call resulting in garbage piling up after a while. If I am correct then this should be more of an issue on x-box or windows phone than on PC.

A fix for this could be to not create EventArgs unless absolutely necessary for those calls that might happen very often but use value types instead. Or maybe use a circular buffer or something as an object pool for those eventargs.

I don't know how nuclex or the others do this, myself I have just avoided implementing calls that would occur too often.

• I have removed all event args since, well.. I don't use them. Thanks for the idea. Also, I found where I had my lag come in. It was in my text box update solution. I have updated my question with what my problem was. Nov 23 '12 at 4:15
• Ahh gotcha! You know you can post an answer yourself and mark it as accepted? Nov 23 '12 at 18:13