Click here to Skip to main content
15,917,862 members
Home / Discussions / C#
   

C#

 
GeneralRe: Cron mechanism Pin
OriginalGriff4-Nov-21 1:59
mveOriginalGriff4-Nov-21 1:59 
GeneralRe: Cron mechanism Pin
johnpierwszy4-Nov-21 6:16
johnpierwszy4-Nov-21 6:16 
Questionmissing a meow ? observations of Net#5 C#9 'record' structures in a VS 2019 WinForm project Pin
BillWoodruff3-Nov-21 11:21
professionalBillWoodruff3-Nov-21 11:21 
QuestionC# regarding running multiple task Pin
Mou_kol1-Nov-21 8:15
Mou_kol1-Nov-21 8:15 
AnswerRe: C# regarding running multiple task Pin
OriginalGriff1-Nov-21 22:49
mveOriginalGriff1-Nov-21 22:49 
AnswerRe: C# regarding running multiple task Pin
lmoelleb1-Nov-21 22:53
lmoelleb1-Nov-21 22:53 
AnswerRe: C# regarding running multiple task Pin
Gerry Schmitz2-Nov-21 7:53
mveGerry Schmitz2-Nov-21 7:53 
QuestionC# reading multiple files by multiple thread issue Pin
Mou_kol1-Nov-21 8:13
Mou_kol1-Nov-21 8:13 
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

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

C#
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<Task> tasks = new List<Task>();
        
                for (int i = 1; i <= counter; i++)
                {
                    Task newTask = new Task((x) => GenerateVirusFile(x), i);                
                    tasks.Add(newTask);
                }
        
                foreach (var task in tasks)
                {
                    task.Start();
                }
        
                Task.WaitAll(tasks.ToArray()); 
        
                Console.ReadKey();
            }
        
            public static void GenerateVirusFile(object 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}");
                }
            }
        }


Thanks
QuestionRe: C# reading multiple files by multiple thread issue Pin
Member 153296131-Nov-21 8:29
Member 153296131-Nov-21 8:29 
AnswerRe: C# reading multiple files by multiple thread issue Pin
Randor 1-Nov-21 15:41
professional Randor 1-Nov-21 15:41 
AnswerRe: C# reading multiple files by multiple thread issue Pin
Gerry Schmitz2-Nov-21 7:53
mveGerry Schmitz2-Nov-21 7:53 
AnswerRe: C# reading multiple files by multiple thread issue Pin
primem0ver4-Nov-21 5:47
primem0ver4-Nov-21 5:47 
QuestionThread Label Updating Pin
Member 1540852926-Oct-21 8:15
Member 1540852926-Oct-21 8:15 
AnswerRe: Thread Label Updating Pin
Gerry Schmitz26-Oct-21 8:36
mveGerry Schmitz26-Oct-21 8:36 
GeneralRe: Thread Label Updating Pin
Member 1540852926-Oct-21 9:17
Member 1540852926-Oct-21 9:17 
AnswerRe: Thread Label Updating Pin
OriginalGriff26-Oct-21 8:59
mveOriginalGriff26-Oct-21 8:59 
GeneralRe: Thread Label Updating Pin
Member 1540852926-Oct-21 9:15
Member 1540852926-Oct-21 9:15 
GeneralRe: Thread Label Updating Pin
OriginalGriff26-Oct-21 22:17
mveOriginalGriff26-Oct-21 22:17 
GeneralRe: Thread Label Updating Pin
Gerry Schmitz28-Oct-21 6:43
mveGerry Schmitz28-Oct-21 6:43 
GeneralRe: Thread Label Updating Pin
OriginalGriff28-Oct-21 20:17
mveOriginalGriff28-Oct-21 20:17 
AnswerRe: Thread Label Updating Pin
BillWoodruff26-Oct-21 16:55
professionalBillWoodruff26-Oct-21 16:55 
Question.NET with problems Pin
Ismael Oliveira 202123-Oct-21 9:48
Ismael Oliveira 202123-Oct-21 9:48 
AnswerRe: .NET with problems Pin
Dave Kreskowiak23-Oct-21 12:15
mveDave Kreskowiak23-Oct-21 12:15 
AnswerRe: .NET with problems Pin
Gerry Schmitz23-Oct-21 16:00
mveGerry Schmitz23-Oct-21 16:00 
AnswerRe: .NET with problems Pin
BillWoodruff23-Oct-21 21:04
professionalBillWoodruff23-Oct-21 21:04 

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.