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
Rate this:
Please Sign up or sign in to vote.

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
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberArun S J18-Nov-12 22:42 
GeneralRe: My vote of 5 PinmemberAnwaar Ahmed18-Nov-12 22:52 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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