Click here to Skip to main content
15,881,709 members
Articles / General Programming / Threads
Tip/Trick

Performing the Same Task With and Without Threads

Rate me:
Please Sign up or sign in to vote.
4.57/5 (4 votes)
18 Nov 2012CPOL2 min read 16.8K   7   4
Performing the same task with and without using threads

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:

C#
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).

C#
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
stsmirnovsv19-Nov-12 10:13
stsmirnovsv19-Nov-12 10:13 
GeneralRe: My vote of 2 Pin
Anwaar Ahmed19-Nov-12 11:04
Anwaar Ahmed19-Nov-12 11:04 
GeneralMy vote of 5 Pin
Arun S J18-Nov-12 22:42
Arun S J18-Nov-12 22:42 
GeneralRe: My vote of 5 Pin
Anwaar Ahmed18-Nov-12 22:52
Anwaar Ahmed18-Nov-12 22:52 

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

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