Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# reading multiple files by multiple thread issue

C# reading multiple files by multiple thread issue

Scheduled Pinned Locked Moved C#
helpcsharpquestioncareer
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mou_kol
    wrote on last edited by
    #1

    Here is two approaches to read multiple files by a multiple threads but getting problem. please tell me what is the problem in the below code

    for (int i = 0; i < counter; i++)
    {
    var thread = new Thread(() => GenerateVirusFile(i));
    thread.Start();
    }

    please see the full code and tell me what is wrong there.

    class Program
    {
    static string folderPath;
    static readonly string fileContent = @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";

        static void Main(string\[\] args)
        {
            folderPath = "F:\\VirusScan";
    
            int counter = 1000;
            for (int i = 0; i < counter; i++)
            {
                var thread = new Thread(() => GenerateVirusFile(i));
                thread.Start();
            }
    
            Console.ReadKey();
        }
    
        static void GenerateVirusFile(int i)
        {
            string filePath = $@"{folderPath}\\TestForVirusScan\_{i}\_{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.txt";
    
            try
            {
                using (StreamWriter writer = new StreamWriter(filePath))
                {
                    writer.WriteLine(fileContent);
                }
    
                var timer = Stopwatch.StartNew();
                while (true)
                {
                    if (!File.Exists(filePath))
                    {
                        Console.WriteLine($"{i}: File was removed in {timer.ElapsedMilliseconds}ms");
                        break;
                    }
                    else
                    {
                        Thread.Sleep(1);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{i}: Exception {ex.GetType().Name} occurred: {ex.Message}");
            }
        }
    }
    

    doing same job Using task

    class Program
    {
    static string folderPath;
    static readonly string fileContent = @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";

            static void Main(string\[\] args)
            {
                folderPath = "F:\\VirusScan";
        
                int counter = 1000;
                List tasks = new List();
        
                for (int i = 1; i <= counter; i++)
                {
                    Task
    
    M L P 4 Replies Last reply
    0
    • M Mou_kol

      Here is two approaches to read multiple files by a multiple threads but getting problem. please tell me what is the problem in the below code

      for (int i = 0; i < counter; i++)
      {
      var thread = new Thread(() => GenerateVirusFile(i));
      thread.Start();
      }

      please see the full code and tell me what is wrong there.

      class Program
      {
      static string folderPath;
      static readonly string fileContent = @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";

          static void Main(string\[\] args)
          {
              folderPath = "F:\\VirusScan";
      
              int counter = 1000;
              for (int i = 0; i < counter; i++)
              {
                  var thread = new Thread(() => GenerateVirusFile(i));
                  thread.Start();
              }
      
              Console.ReadKey();
          }
      
          static void GenerateVirusFile(int i)
          {
              string filePath = $@"{folderPath}\\TestForVirusScan\_{i}\_{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.txt";
      
              try
              {
                  using (StreamWriter writer = new StreamWriter(filePath))
                  {
                      writer.WriteLine(fileContent);
                  }
      
                  var timer = Stopwatch.StartNew();
                  while (true)
                  {
                      if (!File.Exists(filePath))
                      {
                          Console.WriteLine($"{i}: File was removed in {timer.ElapsedMilliseconds}ms");
                          break;
                      }
                      else
                      {
                          Thread.Sleep(1);
                      }
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine($"{i}: Exception {ex.GetType().Name} occurred: {ex.Message}");
              }
          }
      }
      

      doing same job Using task

      class Program
      {
      static string folderPath;
      static readonly string fileContent = @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";

              static void Main(string\[\] args)
              {
                  folderPath = "F:\\VirusScan";
          
                  int counter = 1000;
                  List tasks = new List();
          
                  for (int i = 1; i <= counter; i++)
                  {
                      Task
      
      M Offline
      M Offline
      Member_15329613
      wrote on last edited by
      #2

      What is wrong?

      1 Reply Last reply
      0
      • M Mou_kol

        Here is two approaches to read multiple files by a multiple threads but getting problem. please tell me what is the problem in the below code

        for (int i = 0; i < counter; i++)
        {
        var thread = new Thread(() => GenerateVirusFile(i));
        thread.Start();
        }

        please see the full code and tell me what is wrong there.

        class Program
        {
        static string folderPath;
        static readonly string fileContent = @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";

            static void Main(string\[\] args)
            {
                folderPath = "F:\\VirusScan";
        
                int counter = 1000;
                for (int i = 0; i < counter; i++)
                {
                    var thread = new Thread(() => GenerateVirusFile(i));
                    thread.Start();
                }
        
                Console.ReadKey();
            }
        
            static void GenerateVirusFile(int i)
            {
                string filePath = $@"{folderPath}\\TestForVirusScan\_{i}\_{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.txt";
        
                try
                {
                    using (StreamWriter writer = new StreamWriter(filePath))
                    {
                        writer.WriteLine(fileContent);
                    }
        
                    var timer = Stopwatch.StartNew();
                    while (true)
                    {
                        if (!File.Exists(filePath))
                        {
                            Console.WriteLine($"{i}: File was removed in {timer.ElapsedMilliseconds}ms");
                            break;
                        }
                        else
                        {
                            Thread.Sleep(1);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{i}: Exception {ex.GetType().Name} occurred: {ex.Message}");
                }
            }
        }
        

        doing same job Using task

        class Program
        {
        static string folderPath;
        static readonly string fileContent = @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";

                static void Main(string\[\] args)
                {
                    folderPath = "F:\\VirusScan";
            
                    int counter = 1000;
                    List tasks = new List();
            
                    for (int i = 1; i <= counter; i++)
                    {
                        Task
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Hello, Windows Defender will detect and block the EICAR test file[^]. If you need to write this to disk then you will need to add an exclusion. Configure and validate exclusions based on extension, name, or location | Microsoft Docs[^] Best Wishes, -David Delaune

        1 Reply Last reply
        0
        • M Mou_kol

          Here is two approaches to read multiple files by a multiple threads but getting problem. please tell me what is the problem in the below code

          for (int i = 0; i < counter; i++)
          {
          var thread = new Thread(() => GenerateVirusFile(i));
          thread.Start();
          }

          please see the full code and tell me what is wrong there.

          class Program
          {
          static string folderPath;
          static readonly string fileContent = @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";

              static void Main(string\[\] args)
              {
                  folderPath = "F:\\VirusScan";
          
                  int counter = 1000;
                  for (int i = 0; i < counter; i++)
                  {
                      var thread = new Thread(() => GenerateVirusFile(i));
                      thread.Start();
                  }
          
                  Console.ReadKey();
              }
          
              static void GenerateVirusFile(int i)
              {
                  string filePath = $@"{folderPath}\\TestForVirusScan\_{i}\_{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.txt";
          
                  try
                  {
                      using (StreamWriter writer = new StreamWriter(filePath))
                      {
                          writer.WriteLine(fileContent);
                      }
          
                      var timer = Stopwatch.StartNew();
                      while (true)
                      {
                          if (!File.Exists(filePath))
                          {
                              Console.WriteLine($"{i}: File was removed in {timer.ElapsedMilliseconds}ms");
                              break;
                          }
                          else
                          {
                              Thread.Sleep(1);
                          }
                      }
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine($"{i}: Exception {ex.GetType().Name} occurred: {ex.Message}");
                  }
              }
          }
          

          doing same job Using task

          class Program
          {
          static string folderPath;
          static readonly string fileContent = @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";

                  static void Main(string\[\] args)
                  {
                      folderPath = "F:\\VirusScan";
              
                      int counter = 1000;
                      List tasks = new List();
              
                      for (int i = 1; i <= counter; i++)
                      {
                          Task
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Reading multiple files at the same time is slower than reading them one after another. Limited channels. Context / thread switching. HDD head contention.

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          1 Reply Last reply
          0
          • M Mou_kol

            Here is two approaches to read multiple files by a multiple threads but getting problem. please tell me what is the problem in the below code

            for (int i = 0; i < counter; i++)
            {
            var thread = new Thread(() => GenerateVirusFile(i));
            thread.Start();
            }

            please see the full code and tell me what is wrong there.

            class Program
            {
            static string folderPath;
            static readonly string fileContent = @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";

                static void Main(string\[\] args)
                {
                    folderPath = "F:\\VirusScan";
            
                    int counter = 1000;
                    for (int i = 0; i < counter; i++)
                    {
                        var thread = new Thread(() => GenerateVirusFile(i));
                        thread.Start();
                    }
            
                    Console.ReadKey();
                }
            
                static void GenerateVirusFile(int i)
                {
                    string filePath = $@"{folderPath}\\TestForVirusScan\_{i}\_{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.txt";
            
                    try
                    {
                        using (StreamWriter writer = new StreamWriter(filePath))
                        {
                            writer.WriteLine(fileContent);
                        }
            
                        var timer = Stopwatch.StartNew();
                        while (true)
                        {
                            if (!File.Exists(filePath))
                            {
                                Console.WriteLine($"{i}: File was removed in {timer.ElapsedMilliseconds}ms");
                                break;
                            }
                            else
                            {
                                Thread.Sleep(1);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"{i}: Exception {ex.GetType().Name} occurred: {ex.Message}");
                    }
                }
            }
            

            doing same job Using task

            class Program
            {
            static string folderPath;
            static readonly string fileContent = @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";

                    static void Main(string\[\] args)
                    {
                        folderPath = "F:\\VirusScan";
                
                        int counter = 1000;
                        List tasks = new List();
                
                        for (int i = 1; i <= counter; i++)
                        {
                            Task
            
            P Offline
            P Offline
            primem0ver
            wrote on last edited by
            #5

            If I were you and for some reason I needed to read/write multiple files in parallel (though Gerry is right when it comes to writing files to traditional hard disk drives), instead of the methods you use, I would simplify things by using the Parallel.For or the Parallel.Foreach method.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups