Read more about Chris here
Monday, September 10, 2007
Mega-Dorky Nerd
Read more about Chris here
Monday, September 3, 2007
Work, Servers & Play
Just a quick update guys & girls. Yes the blog has been a bit dormant, but that's because I'm tied up three ways.
- Work has picked up and I'm tied up with that.
- I'm working on a multi-threaded simple http server for the next example. Bear with me J
- I'm building my own website, which I will use as the basis for some new web app lessons.
Check back in a few days, you ought to see something nice.
Salam
Wednesday, August 29, 2007
Programmer Personality Type
Your programmer personality type is:
PHSB
You're a Planner.
You may be slow, but you'll usually find the best solution. If something's worth doing, it's worth doing right.
You like coding at a High level.
The world is made up of objects and components, you should create your programs in the same way.
You work best in a Solo situation.
The best way to program is by yourself. There's no communication problems, you know every part of the code allowing you to write the best programs possible.
You are a liBeral programmer.
Programming is a complex task and you should use white space and comments as freely as possible to help simplify the task. We're not writing on paper anymore so we can take up as much room as we need.
Tell me what type you are. Link.
Salam
On the bizarre side…
Ok, this is post on slashdot is simply absurd. Here's the link: http://yro.slashdot.org/article.pl?sid=07/08/29/0227251&from=rss
I'm REALLY interested in reading your comments.
Salam
Tuesday, August 28, 2007
Multi-Threading in .NET – Lesson 3: The Wonderful World of the ThreadPool
In the previous lessons, we've covered the basic ground work for threading both as a background thread (in lesson 2) and as a new foreground thread (in lesson 1). In this lesson, I'll begin discussing one of my favorite ways of threading available to developers in the .NET framework, namely, the ThreadPool.
Essentially, the ThreadPool is what its name says it is a pool of Threads. But the ThreadPool is really a smart object; As soon as one of the ThreadPool [TP] threads completes its work/tasks, the thread expires and is returned to the TP where it is recycled back into the pool as a prospective new thread available to do your background bidding. The number of threads for the process is usually fixed depending on the amount of memory available, and the needs of the application. However, it might be possible to increase the number of available threads. There is always one and only one TP available to any process.
If you're a Win32 veteran you will realize that there is quite a difference between Multi-Threaded applications there and ones in the CLR. In Win32 [Multi-Threaded] applications, each process was made up of a group of threads and each thread consisted of TLS (Thread Local Storage) and Call Stacks to provide time slices for single processor computers. Single processor machines allot time slices for each thread to execute based on the thread priority. When the time slice for a particular thread has reached completion, the thread is suspended and another thread is allowed to begin its processing. CLR Multi-Threading is, again, different. For each task queued in the thread pool (work items), the CLR assigns a thread from the pool (a worker thread) and then recycles the thread back into the pool once the thread's works has been completed.
The TP has a predefined size of 25 threads. Although this can be changed, I've found through practical experience that this number is usually more than sufficient for the majority of applications. In fact, I personally have never needed something larger, though I can envision Server class applications that would need threads in the thousands.
Getting the TP to start a new thread processing is simple really. You first need to define a WaitCallBack, which in the TP's sense really means a delegate that will be the entry point for the worker thread's allotted tasks.
The QueueUserWorkItem method has two prototypes:
- Public Shared Function QueueUserWorkItem (callBack As WaitCallback, state As Object) As Boolean
- Public Shared Function QueueUserWorkItem (callBack As WaitCallback ) As Boolean
The first method accepts an Object that contains any data that might pertain to your worker thread's needs. The example I have provided uses this overload.
[Side Note: Don't forget you can box anything in an Object type… you simply need to cast it back to its original typeJ]
The example I'm providing is quite simple really. It spawns several threads from the TP and emulates a process by looping through a sleep and wake up mechanism. For the newbies, please note that the main thread effectively finished processing, but is 'stuck' at the Console.ReadLine() before the module exits. This is important because if you remove it (try it) the program will exit even though there are other threads (TP threads) still running. The reason is because TP threads are background threads.
Salam
Monday, August 20, 2007
Multi-Threading in .NET – Lesson 2: Background Threads
Carrying on with our primers on Multi-Threading in the .NET framework, we reach the wonderful world of the background thread.
Actions that are performed on the background threads are exactly the same as can be performed on the foreground threads – with 2 differences.
- Foreground threads will have a higher precedence over a background thread with regards to processing time
- When the last foreground thread of a process is stopped, then all the [remaining] background threads launched by the process will stop as well.
I've seen MANY .NET developers make the mistake of using foreground/background thread interchangeably, along with the usage of process and thread. This is what my mother used to tell me is a "BIIIG no no!".
Quick Definition:
A process is a complete application which might run on one (Single Thread) or many (Multi Thread) threads depending on the application design and requirements. In most multithreading operating systems, a process gets its own memory address space; a thread doesn't. Threads typically share the heap belonging to their parent process.
Essentially, creating a background thread is for 2 main reasons.
- You want to run a long running process that would normally lockup the main process which is usually the UI
- You want the process to follow suit with the state of the originating process, so that if you end the application, the background process is killed.
Let's look at the following example application. It's a Windows app that touches ALL the files on a selected drive including all sub folders. This is very time consuming. On my D: drive, the process took over ten minutes and still wasn't complete. Running this as a single thread would lock up the front end and make it unresponsive, rendering the program completely useless until the touching of all the files is complete. You can get a look at this single threaded version of the application here.
As you can see, the front end simply freezes. Running the IO process on a different background thread will unfreeze the frontend as so here. In fact, to prove the point, I've added another button titled Action which when you click will simply pop up a MessageBox and state the time to the second whence it was clicked.
You will notice that Background threads share almost everything (less what was mentioned before) with normal threads. The key difference is by setting the Thread instance's property IsBackGround to True.
Another thing that you should note (something that I'll be blogging about soon) is the need to run the ListBox's Invoke method. This is to make sure that cross thread calls are safe (since we have an originator thread and a background thread, we're now multi-threaded!)
The next time, I'll be talking about the ThreadPool and just how great and simple multi-threading becomes using it.
Mustafa Ismail
Sunday, August 19, 2007
Multi-Threading in .NET – Lesson 1: The Basics
It seems that many don't have a firm grasp on multi-threading. I'll be covering across a series of several lessons almost all the facets of multi-threading. I'll start off with a definition of multi-threading and then I'll show the basic functions available in the APIs before I move on to the more advanced types.
Firstly, a good definition of Multi-Threading is :
"it is easy to confuse multi-threading with multitasking or multiprogramming, which are somewhat different ideas.
Multi-threading is the ability of a program or an operating system
process to manage its use by more than one user at a time and to even manage multiple requests by the same user without having to have multiple copies of the program running in the computer. Each user request for a program or system service (and here a user can also be another program) is kept track of as a thread with a separate identity. As programs work on behalf of the initial request for that thread and are interrupted by other requests, the status of work on behalf of that thread is kept track of until the work is completed. " (from WhatIs.com)
At the heart of Multi-Threading (from the .NET framework's perspective) we have the System.Threading.Thread class. This class allows us to create a new thread which would operate in parallel to the main application thread that spawned (created) it. An extremely simple use could be as follows:
Imports System.Threading
Module modStart
'entry point of the Console Application
Sub Main()
Dim thread As New Thread(AddressOf clsWork.DoWork)
thread.Start()
'do primary thread work
End Sub
End Module
'Class that does the work on the spawned thread
Class clsWork
Shared Sub DoWork()
'Do spawned thread work
End Sub
End Class
Obviously, this particular example does absolutely nothing, but it shows the basic way of creating and starting a thread. This particular method call does not pass along any new thread parameters and it is very minimalist in providing information about the thread and its functionality. You can find a fully functioning and well documented example solution right here. Keep in mind, this solution only shows the basics of creating a new thread and it is only a guideline.
Tomorrow, I'll be showing you how to create background threads and how they're useful for running long running tasks such as I/O.
Mustafa Ismail
Friday, August 17, 2007
First Blog – The Introduction
Hi all, my name is Mustafa Ismail and I'm a Senior Developer at eSense Software. I'll be blogging all about the technicalities of the .NET framework from a developer's prospective. Most of my tech blogs will be in both C# and VB.NET (though personally I prefer C# due to my C/C++ background).
I sincerely hope you'll enjoy my blog as I take you through the developer's prospective of Microsoft's (most probably) best creation, the .NET framework.
Mustafa Ismail
