You are on page 1of 12

Lesson 20

Threading
Threading
- thread is an independent execution path, able
to run simultaneously with other threads
- saves wastage of CPU cycle and increase
efficiency of an application
- parallel execution of code
- the “main” thread (Thread.CurrentThread)

- System.Threading
Threading
- Thread Life Cycle

• The Unstarted State


• The Ready State
• The Not Runnable State
• The Dead State
Threading
public static void CallToChildThread()
{
Console.WriteLine("Child thread starts");
}

static void Main(string[] args)


{
ThreadStart childref = new ThreadStart(CallToChildThread);
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(childref);
childThread.Start();
Console.ReadKey();
}
Threading
class ThreadTest{
static void Main() {
Thread t = new Thread (WriteY); // Kick off a new thread
t.Start(); // running WriteY()

// Simultaneously, do something on the main thread.


for (int i = 0; i < 1000; i++) Console.Write ("x");
}

static void WriteY()


{
for (int i = 0; i < 1000; i++) Console.Write ("y");
}
}
Threading
public static void CallToChildThread(){
Console.WriteLine("Child thread starts");
int sleepfor = 5000;
Console.WriteLine("Child Thread Paused for {0} seconds", sleepfor / 1000);
Thread.Sleep(sleepfor);
Console.WriteLine("Child thread resumes");
}
static void Main(string[] args){
ThreadStart childref = new ThreadStart(CallToChildThread);
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(childref);
childThread.Start();
Console.ReadKey();
}
public static void CallToChildThread(){
try{
Console.WriteLine("Child thread starts");
for (int counter = 0; counter <= 10; counter++){
Thread.Sleep(500);Console.WriteLine(counter);
}Console.WriteLine("Child Thread Completed");
}
catch (ThreadAbortException e){Console.WriteLine("Thread Abort Exception");}
finally{Console.WriteLine("Couldn't catch the Thread Exception");}}

static void Main(string[] args){


ThreadStart childref = new ThreadStart(CallToChildThread);
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(childref);
childThread.Start(); Thread.Sleep(2000);
Console.WriteLine("In Main: Aborting the Child thread");
childThread.Abort(); Console.ReadKey();
}
BackgroundWorker
class Program{
static BackgroundWorker _bw = new BackgroundWorker();
static void Main() {
_bw.DoWork += bw_DoWork;
_bw.RunWorkerAsync ("Message to worker");
Console.ReadLine();
}
static void bw_DoWork (object sender, DoWorkEventArgs e)
{
// This is called on the worker thread
Console.WriteLine (e.Argument); // writes "Message to worker"
// Perform time-consuming task...
}
}
Regular Expressions
- pattern that could be matched against an
input text
- pattern consists of one or more character
literals, operators, or constructs

http://msdn.microsoft.com/en-us/library/az24scfc.aspx
Regular Expressions
static void Main(string[] args)
{
string str = "A Thousand Splendid Suns";
MatchCollection mc = Regex.Matches(str, @"\bS\S*");
Console.WriteLine("Matching words that start with 'S': ");
foreach (Match m in mc)
{
Console.WriteLine(m);
}
Console.ReadKey();
}
Regular Expressions
static void Main(string[] args){
string input = "Hello World ";
string pattern = "\\s+";
string replacement = " ";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);

Console.WriteLine("Original String: {0}", input);


Console.WriteLine("Replacement String: {0}", result);
Console.ReadKey();
}
Task
http://rss.weather.com/weather/rss/local/MKXX0005?cm_ven=LWO&cm_cat=rss&par=LWO_rss

Create application that will record each change in the feed


and log the weather change in local database or xml file.

The application should then be able to give current weather


conditions and average temperature for selected period.

The application should run all time in system tray.

You might also like