Writing an attribute, thread and exception handling C# programs

Posted by in C# category on for Intermediate level | Points: 250 | Views : 12084 red flag
Rating: 5 out of 5  
 1 vote(s)

We will use advanced features such as attributes, threads, and exception handling.

Let's write a C# programs that use advanced features.

Using Threads

Suppose you want to create a simple C# console application that uses multiple threads to simultaneously count up to and down from ten. The first step in writing the program is to declare the namespaces from which you will inherit.

Step 1 of 7

This program will use some methods that are not defined in the System namespace.

Type the name of the .NET namespace that includes support for multitasking.

using System;

You type System.Threading, because this namespace contains the classes used to support multitasking.

Step 2 of 7

This code describes the count-up-to-ten method in the program. You want the thread to pause after each iteration through the for loop to allow other threads to execute.

using System;
using System.Threading;

namespace Threads
    class TestThreading
        static void CountMethod()
            for (int i = 1; i < 10; i++)
                Console.WriteLine("{0} : Count: {1}", Thread.CurrentThread.Name, i);
                MISSING CODE



The Thread.Sleep method suspends the thread for the amount of time specified.

Step 3 of 7

The CountDown method is enclosed in a try block in case it encounters a problem.

static void CountDownMethod()
                for (int i = 10; i >= 0; i--)
                    Console.WriteLine("{0} : CountDown:{1}",Thread.CurrentThread.Name, i);
            catch (MISSING CODE}

The CountDown method will throw an exception called ThreadInterruptedException if it fails, so this is the exception the catch clause should handle.

Step 4 of 7

Now that the count-down-to-ten and count-up-to-ten methods have been defined, the threads that carry out these methods must be called.

Enter the name of the delegate that will handle the Start event of the Thread class.
static void Main(string[] args)
            MISSING CODE
            Console.WriteLine("Creating count thread");

You type ThreadStart as the name of the delegate that will handle the Start event of the Thread class.

Step 5 of 7

The ThreadStart delegate will create a new thread, but not start it.

Enter the code needed to start the new thread t1.
static void Main(string[] args)
            ThreadStart count = new ThreadStart(CountMethod);
            Console.WriteLine("Creating count thread");

            ThreadStart countdown = new ThreadStart(CountDownMethod);
            Console.WriteLine("Creating countdown thread");

            Thread t1 = new Thread(count);

            t1.Name = "thread 1";

            MISSING CODE;

The new thread is started with the Start method. So you enter the code t1.Start() in this case.

Step 6 of 7

Sometimes, one thread can be executed properly only if another thread has finished.

Thread t2 = new Thread(countdown);
            t2.Name = "thread 2";

            Console.WriteLine("Starting {0}", t2.Name);

            for (int i = 1; i < 10; i++)
                Console.WriteLine("Main Method: {0}", i);

            MISSING CODE

Calling the Thread.Join method means that the current thread will wait until the specified thread has finished before it continues processing. So in this case, you enter the code t1.Join();.

Step 7 of 7

Sometimes, you will want a thread to pause for an unspecified period of time.

Which option pauses the thread until it is woken by the Resume method?
            Console.WriteLine("Thread 1 has finished processing!!!");

            Console.WriteLine("Thread 2 has finished processing!!!");

            Thread t3 = new Thread(count);
            t3.Name = "thread 3";

            Console.WriteLine("Starting {0}", t3.Name);

            MISSING CODE

The Suspend method of the Thread class will pause the thread until it's called by the Resume method. So in this case, you enter the code t3.Suspend();.

The output of the code shows the two alternating methods.

Creating count thread

Creating countdown thread

Starting thread 1

thread 1 : Count: 1

Starting thread 2

Main Method: 1

thread 2 : CountDown: 10

thread 2 : CountDown: 9

Main Method: 2

thread 1 : Count: 2

thread 2 : CountDown: 8

Main Method: 3

thread 1 : Count: 3

thread 2 : CountDown: 7

Main Method: 4

thread 1 : Count: 4

thread 2 : CountDown: 6

Main Method: 5

thread 1 : Count: 5

thread 2 : CountDown: 5

Main Method: 6

thread 1 : Count: 6

thread 2 : CountDown: 4

Main Method: 7

thread 1 : Count: 7

thread 2 : CountDown: 3

Main Method: 8

thread 1 : Count: 8

thread 2 : CountDown: 2
Thanks for reading.
Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: John Doe
Member Level: Bronze
Member Status: Member,MVP
Member Since: 1/22/2011 7:38:35 AM
Country: India

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)