Click here to Skip to main content
Click here to Skip to main content

Performing the Same Task With and Without Threads

By , 18 Nov 2012
 

Introduction

This tip demonstrates an example of performing the same task, both with threads and without threads. You will see the differences after compiling the code.

We shall write a simple console application that reads text files from D:\Test. To perform this task, we write a function "ReadFile" which will read a single file line by line and write it to console. We shall call this function once for each file.

First, we shall call this function without using threads and then we shall call it using threads.

Using the Code

First, let's save three text files (caps.txt, smalls.txt, nums.txt) in a directory D:\Test.

Now create a Console Application with the following code.

Calling a Function Without Using Threads

Our function "ReadFile" reads a file line by line and writes it to console. In this code, we shall call this function without using threads. We are simply going to call it three times, i.e., once for each file. In this way, each file will be read and written to the console, one after another, i.e., the second file will start processing only when the first file is finished.

Here is the sample code:

using System;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication1
{
    public class TestWitoutThreads
    {
        static string path = "D:\\Test\\";

        public static void Main()
        {
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] files = di.GetFiles("*.txt");
            
            //Read first file
            ReadFile(files[0]);
            //Read second file
            ReadFile(files[1]);
            //Read third file
            ReadFile(files[2]);
           
            Console.ReadKey();
        }

        private static void ReadFile(FileInfo file)
        {
            StreamReader sr = new StreamReader(path + file.Name);
            string line = sr.ReadLine();
            while (line != null)
            {
                Console.WriteLine(line);
                line = sr.ReadLine();
            }
            sr.Close();
        }
    }
}

Calling a Function using Threads

Now, we shall call the same function using threads. We shall create three threads for the three files and so all three files will be read and written to the console simultaneously because there will be three threads running at the same time performing the same task (i.e. executing the same function using threads).

using System;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication1
{
    public class TestWithThreads
    {
        static string path = "D:\\Test\\";

        public static void Main()
        {
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] files = di.GetFiles("*.txt");
            
            //create threads
            Thread[] threads = new Thread[3];
            threads[0] = new Thread(delegate() { ReadFile(files[0]);});
            threads[1] = new Thread(delegate() { ReadFile(files[1]);}); 
            threads[2] = new Thread(delegate() { ReadFile(files[2]);});
            
            //start threads
            threads[0].Start();
            threads[1].Start();
            threads[2].Start();

            Console.ReadKey();
        }

        private static void ReadFile(FileInfo file)
        {
            StreamReader sr = new StreamReader(path + file.Name);
            string line = sr.ReadLine();
            while (line != null)
            {
                Console.WriteLine(line);
                line = sr.ReadLine();
            }
            sr.Close();
        }
    }
}

Points of Interest

We will see that each time we call function "ReadFile" without using threads, the files will be processed one by one. On the other hand, when we shall call the same function using threads, all three files will be processed simultaneously simply because three threads will be running simultaneously.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Anwaar Ahmed
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 2memberMember 871213719 Nov '12 - 10:13 
GeneralRe: My vote of 2memberAnwaar Ahmed19 Nov '12 - 11:04 
GeneralMy vote of 5memberArun S J18 Nov '12 - 22:42 
GeneralRe: My vote of 5memberAnwaar Ahmed18 Nov '12 - 22:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 19 Nov 2012
Article Copyright 2012 by Anwaar Ahmed
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid