Back to Basics: AND vs. OR vs. XOR

19. August 2011

We often think of computers as working with numbers, and in the purest sense we are, but those numbers are simply 1s and 0s. Normally these bits can be grouped together to represent much larger numbers but as a developer, you'll often times need to use them as bits and you forego addition and division for "bitwise" (sometimes referred to as Logical) operations. Bitwise simply means that you're dealing with individual bits and not the number as a whole, the representation of the bits working together to make a larger number.

More...

C#, Computer Security, Design Pattern, Development, Misc

Threads: Doing Them Right w/GUI

28. November 2010

I did an article over threads in a command line project. In a command line project, there are less complications than it's GUI alternative. Here, I hope to go into enough detail to put to rest the confusion that threads can introduce to a GUI application. The big hurdle is around the limitation that the only thread to interact with a control is the thread that created the control. In the beginning, I saw this as a silly rule, one not worthy of throwing an exception.  The more and more I use threads, the more obvious it becomes that this is less of a suggestion and more of a golden rule. So, the question begs, how do we create a thread to do something that we need a result back from? How do we get any information from a thread that's been started, such as progress, or state of operation? With callbacks, events, and the Invoke method! If we take some of the concepts I discussed in threaded CLR projects, like a payload struct, and use them here, than this will be an easy concept for you.

To start, I'll say that there is a long-named boolean variable we can set to false in the constructor of a form called "CheckForIllegalCrossThreadCalls." When set to false, is does just what you think, it will not check for illegal cross thread calls. To break down what that means, when true it will check to see if a thread tries to access a control it did not create, and when false, it will allow it. In simple applications this may be all you need, but any amount of complexity and it quickly becomes a bad idea. The problem with threads is their unpredictability. Should two threads happen to be editing the same control, things become unstable. From unintended outputs to crashing programs, it's not something you want to test, it's just better to do it right the first time, and every time. This variable, CheckForIllegalCrossThreadCalls, should remain true, by default it is.

For my little example here, the UI will consist of two progress bars and a button. Upon clicking the button, one thread starts per progress bar and they will update the progress bar they're associated with over time. This will leave the GUI thread to do nothing, at least until each thread calls their invokes. In addition to these controls, the form will have two delegates, two events, and a list of all the running threads. The events and delegates are paired. One pair is to update a progress bar, the other pair is for when a thread completes. There is also a struct to act as the payload for the threads, as defined here:

More...

Advice, C#, Development, Source Code , , ,

Threads: Doing Them Right

8. September 2010

Threads... I'm not going to claim to be an expert at explaining all the low level happenings or all the intricate little bugs and/or features of them. I am going to be talking about good foundation code for making threads work right without worrying about races, locks, or GUI halts. First, a basic understanding of what a thread is. A thread allows for multiple lines of execution to be running in one application at the same time. You may need this for many different reasons. With only one thread, that thread is in charge of what's called the message loop. It'll listen and respond to events such as repainting the form and user interactions (clicking a button, dragging the window), if you put that thread to use doing something that takes a lot of time (say you click a button and that does a lot of network traffic or image processing), then the message loop goes unattended, this is how Windows decides if an app is "Not Responding" (when the message loop accumulates events) until it completes whatever it's doing, then it begins responding to the message loop again. With another thread, you can handle the heavy load and the original thread can continue responding to events in the message loop. This can be both a good thing and a bad thing.

There are many problems that can arise in multi-threaded application. Let's say you had an array, and in one thread you're getting element 6 in that array and in another array you're inserting an element into index 2. Different things could happen based on which thread gets there first, and it's impossible to tell which will. You can always use a lock statement to prevent data corruption due to multiple threads accessing it, but that can lead to thread locks and isn't always the easiest thing to do. Instead, design your application to be thread safe from the ground up. It's easier to hammer out the details to a thread safe app first and then implement it than it is to design a semi-working app and fix the thread issues once they've become prevalent. Defining set rules for how to be thread safe is hard. The knowledge comes with experience in many different applications.

More...

Advice, C#, Development, Source Code , , , ,

A Beginner's Article on Binding

17. August 2010

Windows Presentation Forms (WPF) brought a lot of cool features to C#. Hardware accelerated rendering, XAML backed control layout, and more versatile ways to bind your data to your controls. In WinForms there were ways to bind data base schemes to controls, but all in all it was a pain and rather limited. WPF opens up binding to almost any type. If you don't know what binding is, binding is when a particular property is "bound" to a control such that when the value changes, the control is updated, and in some cases, if the control is changed, the property is updated. I've thrown together a very basic application to help demonstrate some of the basics of binding, let me walk you through it.

More...

C#, Development, Source Code ,

Design Patterns: The Observer Pattern

9. August 2010

From Wikipedia: In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. Design patterns can be a touchy subject. Sometimes described as "a solution seeking a problem," they can complicate projects where they have been implemented incorrectly through misunderstanding the pattern or by being applied to a situation that doesn't warrant that design pattern. With that said, take this article with a grain of salt. Don't look for a place to use it, but keep it as a tool for when a problem that may need it arises. With that disclaimer behind me, let me introduce you all to the Observer Pattern.

More...

C#, Design Pattern, Development, Source Code , , ,

New Case Study Coming: Tower Defense!

20. July 2010

To fill up what little spare time I have, I've started a new project. Nothing ground shattering, I'm not curing cancer or solving the traveling salesman problem in O(n) or anything important like that, but who doesn't love a good Tower Defense game? My favorite TD has always been Bloons Tower Defense, and while playing it, I can't help but think about how I would do things from a development point of view on the back-end of the game. What tower designs would I come up with? How would I design the path(s) on a level? What creature/enemy/bloon design would I have crawling that path? Could I twist the game with a new rule or concept added in? Could it be made into a head to head multiplayer style game? My mind is flooded with the possibilities of what to include in the game! I'll be keeping track of my progress as I go and I'll post everything along with the end result at the same time, source code and all. I've already started developing fundamental aspects to the game. You can place points to draw a path and little dots currently crawl the path before disappearing at the final point. Exciting I know, but you have to walk before you crawl and crawl before you kill it with a tower.

This is my first Tower Defense game to ever make so we get to walk the path together when I finish writing up the whole article for you. The design process is moving along and today I plan to talk specifications with my lead art designer (my buddy Brice) about what visual themes we want to give the game, he'll definitely be in charge of that. I plan to go over details such as how I'm going to track of a path for the enemies, what strengths/weaknesses to give my towers/enemies, what choices I give the player, how difficulty settings change gameplay, the money and points systems, and every detail I can come up with down to the name of the project (currently undecided). I don't have too many things set in stone, yet. All I can promise is that it's going to be in C# and optimistically take a good two or three weeks. I'm sure you'll hear back from me before it's done, though. This is the first of many case study programs I'll be doing so don't forget to come back and check out what's available.

C#, Case Study, Development , ,

Wrapfield, Source Code and All

15. July 2010

I'm going to begin putting up some of my projects for you all to read, run, and use. First up, Wrapfield! A twist on the traditional click and flag game, Minesweeper has been taken to a new level with a new rule alongside all the original features. Take the code, learn from it, play the game, do what you want to do, but of course I ask you leave my name (and my buddy's name, Shawn) left in the about dialog box. You can find it on the right hand side under Pages or by clicking here.

More projects are on the way as I clean them up and prepare them for the site, although not all will contain source code such as this one. Also, I want to begin writing multi-part case studies where we walk together through complex projects facing decisions, possible fixes and their flaws, as well the optimal solutions for those problems. With complete access to the source code of the final project, and all the reasons behind the code as the project expands. Keep an eye out over the coming weeks as more projects and these case studies come to the site. Have a particular idea for a case study? Propose the idea in the comments, if I like it I may use it!

C#, Project, Source Code , ,