Tuesday, March 11, 2008

Threading Sample in C# 2.0

Here is sample for Threading concept in C# 2.0.

First, Create a web application

The Namespace to be used is System.Threading;
We create two methods Threadjob & Threadjob1.And Inside Page_Load we create objects ts1 & ts for ThreadStart. ThreadStart is a Delegate for System.Threading.Thread. And we create objects
T & T1 for class Thread & Start them. Now to understand its operation place the breakpoints above the commented places in the code given below. Based on the sleep time & number of threads you will see the flow jumping here & there... thats how its works :)

Followign is its code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
ThreadStart ts = new ThreadStart(Threadjob);
ThreadStart ts1 = new ThreadStart(Threadjob1);
Thread T = new Thread(ts);
Thread T1 = new Thread(ts1);
void Threadjob()
for (int l = 0; l <= 10; l++)
Response.Write("\n Method " + l.ToString());
//Have Break point #1 Here...

void Threadjob1()
for (int l1 = 0; l1 <= 10; l1++)
Response.Write("\n 2nd Method " + l1.ToString());
//Have Break point #2 Here...


No comments: