This is actually something I did research on a while back (6 months or so). The ThreadPool object in .NET is a fairly powerful concept which reuses existing threads registered to the pool and allocates new threads as necessary dependent on your machine specs. Like all High Level objects this has some overhead to it, but probably less than what most could create on their own with exactly the same behavior.
Friday, February 17, 2012
ThreadPool vs Custom Pool : MultiPipeThread
This is actually something I did research on a while back (6 months or so). The ThreadPool object in .NET is a fairly powerful concept which reuses existing threads registered to the pool and allocates new threads as necessary dependent on your machine specs. Like all High Level objects this has some overhead to it, but probably less than what most could create on their own with exactly the same behavior.
Wednesday, February 15, 2012
Polymorphism: Structs, Interfaces, and Huh?
Ok, I know odd title, but I promise this will all get explained.
First, let’s start with some definitions of these concepts.
Polymorphism-
I like the Wiki version of this definition so here’s the link: http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
For those who prefer not to click links and stay here, I will explain in a 1000 foot overview way. Basically in object-oriented programming, this is the ability to create a value, variable, function, or object that has more than one form. Shapeshifting so to speak, just as it would mean if not referring to coding. WOW! Simple enough right?
Structs-
MSDN does a fairly good job describing this:
http://msdn.microsoft.com/en-us/library/saxz13w4.aspx
Here, a struct is essentially the same as a class but it’s a value type. Just a little more limited, but can be less expensive, especially when using smaller objects that may be contained in large collections for example.
To get deep with it, a class is allocated in memory and allocates pointers (IntPtr) to the object wherever you place a field for reference type objects, classes.
However, structs, or value types, are allocated where you place them and do not generate pointers. This does mean they will copy and become unique allocations moving through the call stack and around the different scopes of your application, this can make them both easier and more difficult to control.
Interfaces-
MSDN also does their job well here:
http://msdn.microsoft.com/en-us/library/ms173156.aspx
Interfaces can be a complex topic for some, but to try to sum it up as simple as possible. They contain a reference to an object which implements the same methods, properties events that the interface defines. If you’ve dealt with C++ these will look a lot like class definitions in header files, and ALMOST behave like them. Interfaces cannot be instantiated and cannot define code implementation for it’s members.
Alright, Polymorphism in general is a huge topic of coding, so we’re not getting too deep, just showing one AMAZING example, though it may be hard to see all the benefits without trying it yourself.
If you’ve dealt with structs before you’ll know a few things about them, such as the inability to use inheritance, and the frustrating ref keyword when trying to pass them into methods as a reference type instead. So, their benefits typically get outweighed by the aggravations of these things. Here’s a tidbit that a lot of people forget, though structs cannot inherit they can implement an interface. It gets better though. Interfaces can inherit other interfaces….
Due to the two above facts, we can use polymorphism techniques to build a collection of structs which inherit each other, and can be passed as reference types without the ref keyword.
My example, which I’ve included the class diagram for, uses the following structs and interfaces:
- IVector
- IVector2
- IVector3
- IVector4
- Vector2
- Vector3
- Vector4

Since the structs, the one’s without the ‘I’, cannot inherit each other all the inheritance tree is in the IVector-IVector4, and it just goes incrementally.
Ok, got that part, but what does this allow me to do?
void RandomMethod( IVector aV1 ) { }
The above method declaration accepts IVector, so what can be passed into this method? This method can actually accept ANY Vector type as it’s interface description. It could use IVector.AxisCount to determine which one was actually passed and handle it appropriately.
That’s one use, which is pretty cool. What about this one?
Vector4 v = new Vector4( 1,1,1,1 );
IVector2 v2 = v;
Will this even work? YES, this just gave me a reference to the Vecto4, the object is obviously still the same object, but rather than converting the value, we made it accessible as a Vector2 interface. Of course you would need methods that accept the interface instead of the struct to use it. Hence why IVector2.ToVector2() exists. This is a simple convert, where if the internal object is not a Vector2, it converts it before returning it.
I know this is a lot to take in so I will stop here. Have fun!!!
Introduction
I just wanted to take a moment to explain why I’m even creating this blog before making my first real posting. This blog I am going to use to pull all of my development activity into a single location.
Those who know me already and have been watching some of my other posts on the RuneEngine v2 Facebook Page should know I post almost everything there as it is. However, there are things I reserve posting there as they are unrelated to the RuneEngine project or any of it’s inner workings.
So, basically anything I post there or anywhere else, will likely end up here as well.
A little about me though. My name is Danny Helms and I am a full time support agent for an Imaging SDK provider in
You may see me mention use of languages in:
- -C#
- C++
- WPF
- Silverlight
- SOME VB.NET
- Javascript
- May start some Java soon
- ASP.NET is also something I will likely piddle with in near future.
Common SDKs I use:
- Microsoft’s XNA Framework
- WeiFenLou Docking API (May not use this as much as I am moving further into WPF development)
- BEPU Physics Engine
Current Projects:
RuneEngine V2 Projects----
- RuneEditor.API (Simple API for addons to the RuneEditor application)
- RuneEngine (2D)
- RuneEngine (Native)
- RuneEngine (Reach, Mobile friendly)
- RuneEngine (HiDef, the core project and most of my efforts go here)
- BitbucketReporting.API ( Utility API for working with Bitbucket's WCF API, included in RuneEditor.API )