When one programs uses multiple threads to accomplish its tasks, it is called multithreading.
Multithreading doesn’t guaranties the sequence of execution between threads.
Sample Code
When one programs uses multiple threads to accomplish its tasks, it is called multithreading.
Multithreading doesn’t guaranties the sequence of execution between threads.
Sample Code
Managed thread pool, provides efficient use of threads with a pool of background threads managed by system.
Example Code
Microsoft today released Internet Explorer 10 for Windows 7
Find below the download links
Internet Explorer 10 for Windows 7 32-bit Edition and Windows Server 2008 R2 32-bit Edition
http://www.microsoft.com/en-us/download/details.aspx?id=36808&WT.mc_id=rss_alldownloads_devresources
Internet Explorer 10 for Windows 7 64-bit Edition and Windows Server 2008 R2 64-bit Edition
http://www.microsoft.com/en-us/download/details.aspx?id=36806&WT.mc_id=rss_alldownloads_devresources
Managed thread can be either background or foreground. Differences are listed below.
If all the foregrounds are completed, background threads will be automatically closed and application will be closed. By default all applications will have a main foreground thread.
Background threads can be created by setting Thread’s IsBackground property to true. By default, a thread will be created as foreground thread.
var th = new Thread(Greet);
th.Name = "Greeting";
th.IsBackground = true;
Asp.net and web tools 2012.2 is released and available on the web.
Download link: http://go.Microsoft.com/fwlink/?linkid=282650
We can assign a meaningful name to the thread after creating the thread and before starting the thread.
var th = new Thread(Greet); // Create new thread
th.Name = "Greeting";
Information about current thread can be obtained using Thread.CurrentThread static property.
Thread.CurrentThread returns the Thread Object related to current thread.
Thread object gives the following information about the thread.
using System;
using System.Threading;// Current thread info
class Program {
static void Main(string[] args) {
Console.WriteLine("Main Started");
var th = new Thread(Greet); // Create new thread
th.Start(new object[] { "Bob", "Good Morning" }); // Execute Greet method in the created thread
Console.ReadKey();
}private static void Greet(object info) {
Console.WriteLine("Hi {0}, {1}!", (info as object[])[0], (info as object[])[1]);
Console.WriteLine("\tThread Name: " + Thread.CurrentThread.Name);
Console.WriteLine("\tThread Id: " + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("\tIs Background: " + Thread.CurrentThread.IsBackground);
Console.WriteLine("\tIs ThreadPool Thread: " + Thread.CurrentThread.IsThreadPoolThread);
Console.WriteLine("\tIs Alive: " + Thread.CurrentThread.IsAlive);
Console.WriteLine("\tPriority: " + Thread.CurrentThread.Priority);
Console.WriteLine("\tThread State: " + Thread.CurrentThread.ThreadState);
}
}
Main Started
Hi Bob, Good Morning!
Thread Name:
Thread Id: 11
Is Background: False
Is ThreadPool Thread: False
Is Alive: True
Priority: Normal
Thread State: Running
A running thread can be aborted using Thread.Abort method. When we abort a thread, a thread abort exception will be thrown in that thread.
using System;
using System.Threading;// Thread.Abort
class Program {
static void Main(string[] args) {
Console.WriteLine("Main Started");
var th = new Thread(Greet); // Create new thread
th.Start(new object[] { "Bob", "Good Morning" }); // Execute Greet method in the created thread
Thread.Sleep(500);
th.Abort();
Console.WriteLine("Main Completed");
Console.ReadKey();
}private static void Greet(object info) {
try {
Thread.Sleep(1000);
Console.WriteLine("Hi {0}, {1}!", (info as object[])[0], (info as object[])[1]);
Thread.Sleep(1000);
}
catch (ThreadAbortException) {
Console.WriteLine("Greet Aborted");
}
}
}
Output:
Main Started
Main Completed
Greet Aborted
We can wait for a thread to complete, but we cannot wait indefinitely sometimes. .Net allows us to wait for a thread to complete just for a while, using Thread.Join method.
Thread.Join method, receives a timeout in milliseconds and waits up to that period. If it is still running, it stops waiting and returns false, else returns true.
using System;
using System.Threading;class Program {
static void Main(string[] args) {
Console.WriteLine("Main Started");
var th = new Thread(Greet); // Create new thread
th.Start(new object[] { "Bob", "Good Morning" }); // Execute Greet method in the created thread
if (th.Join(800))
Console.WriteLine("Greeting finished");
else
Console.WriteLine("Greeting is still running");
Console.WriteLine("Main Completed");
Console.ReadKey();
}private static void Greet(object info) {
Thread.Sleep(1000);
Console.WriteLine("Hi {0}, {1}!", (info as object[])[0], (info as object[])[1]);
Thread.Sleep(1000);
}
}
Output:
Main Started
Greeting is still running
Main Completed
Hi Bob, Good Morning!
We can make a thread sleep for a specific milliseconds. We can use this for testing the thread and asynchronous programming concepts and also we can use this to wait for a resource.
Example:
using System;
using System.Threading;class Program {
static void Main(string[] args) {
Console.WriteLine("Main Started");
var th = new Thread(Greet); // Create new thread
th.Start(new object[] { "Bob", "Good Morning" }); // Execute Greet method in the created thread
th.Join();
Console.WriteLine("Main Completed");
Console.ReadKey();
}private static void Greet(object info) {
Thread.Sleep(1000);
Console.WriteLine("Hi {0}, {1}!", (info as object[])[0], (info as object[])[1]);
Thread.Sleep(1000);
}
}
Above thread will waiting for 1 second (1000 milliseconds) before printing the greetings and after printing the greetings
We can wait for a thread to complete using the thread’s Join method.
using System;
using System.Threading;class Program {
static void Main(string[] args) {
Console.WriteLine("Main Started");
var th = new Thread(Greet); // Create new thread
th.Start(new object[] { "Bob", "Good Morning" }); // Execute Greet method in the created thread
th.Join();
Console.WriteLine("Main Completed");
Console.ReadKey();
}private static void Greet(object info) {
Console.WriteLine("Hi {0}, {1}!", (info as object[])[0], (info as object[])[1]);
}
}
Join method will block the calling thread until the thread completes.
Instead of creating a class to pass multiple parameters to a thread, we can use Object arrays to be passed to the method.
Example:
using System;
using System.Threading;class Program {
static void Main(string[] args) {
Console.WriteLine("Main Started");
var th = new Thread(Greet); // Create new thread
th.Start(new object[] { "Bob", "Good Morning" }); // Execute Greet method in the created thread
Console.WriteLine("Main Completed");
Console.ReadKey();
}private static void Greet(object info) {
Console.WriteLine("Hi {0}, {1}!", (info as object[])[0], (info as object[])[1]);
}
}
Thread class supports two types of methods
To pass multiple values to Thread, we can create a small class to store multiple parameters and send it as the parameter
Example:
using System;
using System.Threading;class Program {
static void Main(string[] args) {
Console.WriteLine("Main Started");
var th = new Thread(Greet); // Create new thread
th.Start(new GreetInfo() { Name = "Bob", Message = "Good Morning" }); // Execute Greet method in the created thread
Console.WriteLine("Main Completed");
Console.ReadKey();
}private class GreetInfo {
public string Name { get; set; }
public string Message { get; set; }
}private static void Greet(object info) {
Console.WriteLine("Hi {0}, {1}!", (info as GreetInfo).Name, (info as GreetInfo).Message);
}
}
We can pass one argument to thread during thread start
using System;
using System.Threading;class Program {
static void Main(string[] args) {
Console.WriteLine("Main Started");
var th = new Thread(Greet); // Create new thread
th.Start("Good Morning"); // Execute Greet method in the created thread
Console.WriteLine("Main Completed");
Console.ReadKey();
}private static void Greet(object message) {
Console.WriteLine("Hi, {0}!", message);
}
}
public delegate void ParameterizedThreadStart(object obj);
Output
Main Started
Main CompletedHi, Good Morning!
Program in the Sample 00 can be converted to utilize the thread as follows.
using System;
using System.Threading;class Program {
static void Main(string[] args) {
Console.WriteLine("Main Started");
var th = new Thread(Greet); // Create new thread
th.Start(); // Execute Greet method in the created thread
Console.WriteLine("Main Completed");
Console.ReadKey();
}private static void Greet() {
Console.WriteLine("Hi, Good Morning!");
}
}
Output
Main Started
Main CompletedHi, Good Morning!
class Program {
static void Main(string[] args) {
Console.WriteLine("Main Started");
Greet();
Console.WriteLine("Main Completed");
Console.ReadKey();
}
private static void Greet() {
Console.WriteLine("Hi, Good Morning!");
}
}
Above programs calls a static method “Greet” from Main method.
Output
Main Started Hi, Good Morning! Main Completed
Error:
Unable to create the virtual directory. ASP.NET 2.0 has not been registered on the Web server. You need to manually configure you Web server for ASP.NET 2.0 in order for your site to run correctly.
Fix:
Open Command Prompt in Admin mode
Navigate to C:\Windows\Microsoft.Net\Framework\
To identify the framework versions installed, type dir /ad
Navigate to the required framework folder
> cd v2.0.50727
Run asp.net registration tool
> aspnet_regiis /i
This will install the asp.net of the selected framework into IIS
Notes:
.Net framework 2.0, 3.0 and 3.5, uses CLR 2.0
.Net framework 4.0 and 4.5 uses CLR 4.0
Now a days computers, have multiple cores in its processors, capable of executing multiple programming in parallel. Our code should be written to utilize the available resources to provide high performance processing to our clients. To support this .Net provided many classes written on top of windows low level API.
We can write parallel programming using following concepts
we can write asynchronous programming in following modes
<accessmodifier> delegate <methoddeclaration>Example
public delegate int Calc(int a, int b);
<delegatetype> <delegatevariable>Example
Calc c;
public int Add(int x, int y)
{
return x+y;
}
new <delegatetype>(<methodname>)Example
Calc c= new Calc(Add);
<delegatevariable>.Invoke(<parameters>)Example
int x = c.invoke(10, 20);
<delegatevariable>(<parameters>)Example
int x = c(10, 20);
A delegate is a class that can hold a reference to a method.
A delegate class has a signature, and it can hold references only to methods that match its signature.
Base class for all the delegates is Delegate.
When we declare a delegate, it implicitly defines a class derived from System.Delegate. It automatically adds few methods that supports method invocations.