I will begin to post my summaries as part of preparation to take my certification exams. The exam that I'm currently preparing is the Microsoft 70-526, Foundations of the .NET Framework 2.0. I choose to start with threading because is one of the most difficult to me.
What is threading?
Threading is a mechanism that allows you to perform concurrent operations on a single machine. The condition is that the machine has more than one processor. Previously, the threading feature was typically a subject of the server development, but now are more frequently found desktop machines and laptops that are capable of threading.Main classes
The main classes that you will need to work with threading are in the System.Threading namespace, and are the following:- Thread: The Thread class represents a single thread of execution. Between their most important instance methods we found the following: a) Start: Allows to begin the thread's execution. b)Abort: Allows to stop the execution of the thread. c:)Join: Indicates to the machine to wait until the thread's execution is complete. Between the static methods of the Thread class include Sleep, that makes the thread to wait for the miliseconds specified.
- ThreadStart: Is a delegate that allows to make reference to the method that will be invoked into the thread.
Example
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace SimpleThreadingDemo { class Program { static void Main(string[] args) { ThreadStart delegado = new ThreadStart(Counting); Thread hilo1 = new Thread(delegado); Thread hilo2 = new Thread(delegado); hilo1.Start(); hilo2.Start(); hilo1.Join(); hilo2.Join(); Console.Read(); } static void Counting() { Int32 i; for (i = 0; i <= 10; i++) { Console.WriteLine("Iteration:{0}, Thread:{1}", i, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(10); } } } }
No comments:
Post a Comment