C# reading multiple files by multiple thread issue
-
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
-
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
What is wrong?
-
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
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
-
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
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
-
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
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.