As part of DotNetDoc's new "Ask the Doc" format, we are starting our journey by taking a page from the Zen Master himself Scott Hanselman. We will be walking through his list of questions posted in his, "What Great .NET Developers Ought To Know" post. We will not only try to answer the questions but give each a "deep dive" so that we not only can answer the question, but so that we UNDERSTAND the answer. We will go one question at a time until we reach the end. I hope you enjoy, and thanks Scott for the thought provoking questions.
.Net Ought To Know #1 : Describe the difference between a Thread and a Process?
The power of Windows comes from its ability to multi-task. Multitasking allows the running of multiple processes in memory simultaneously. So first we need to define a "process". In its simplest definition, a process is an instance of a program running in a computer. The Process is the object that owns all of the resources of the application it belongs to.
It is important to remember that a process is NOT an application. In the same way that a class is not an object, an application is not a process. An application is the static code and static data, the process is the code "in-action". Each process runs in its own section of virtual memory. This allows the operating system to keep them separate so that a problem with one process does not bring down the whole system. Inside its own section of Virtual Memory, everything needed to run the application, from the code to the stack and heap, to it's static variables , can be found.
A thread on the other hand is an independent path of execution WITHIN a process (thread of execution) . Threads are very similar to processes in that they both represent a sequence of instructions needing to be executed. When you begin a process a "Start Up" thread is created that will execute your Main() method and continue running until the method has ended. A process can create multiple threads (Multi-Threaded Application) WIHTIN the process to perform tasks. A process can be thought of as a collection of threads. It can have several threads running concurrently giving the allusion that your application is running faster. Remember, your operating systems can only do one thing at a time (Excluding machines with multiple processors of course). So having multiple threads allows your program to continue without having to wait for a particular task to complete.