Threads in c++ and c# with Lambda Expressions 29

Опубликовано: 13 Апрель 2026
на канале: Arif Mahmood
138
1

Learn how to work with #functions as #threads. How to use join, sleep and start keywords. How to work with #lambda #expressions in c plus plus and c sharp.


c# code:
using System;
using System.Collections.Generic;//for string
using System.IO;//to use StreamReader
using System.Threading;// for threads
namespace lec2csharp
{
class Program
{
static void Pullout_contents(string in_file, out List<string> elements)
{
StreamReader input_file = new StreamReader(in_file.ToString());
elements = new List<string>() { "" };
elements.Clear();
string line = "";
for (int line_no = 1; (line = input_file.ReadLine()) != null; ++line_no)
{
elements.Add(line);
}
}

static void Show_contents(ref StreamWriter Out, List<string> elements)
{
foreach (string iter in elements)
{
Console.WriteLine(iter);
}
}

static void Pushin_contents(string out_file, List<string> elements)
{
StreamWriter output_file = new StreamWriter(out_file.ToString());
output_file.AutoFlush = true;
Console.SetOut(output_file);
Show_contents(ref output_file, elements);// using polymorphism
}

static void Main(string[] args)
{
StreamReader input_file = null;
StreamWriter output_file = null;

string in_file;
in_file = "test.txt";

string out_file;
out_file = in_file + ".out";

List<string> elements = new List<string>();
elements.Clear();

//Pullout_contents(in_file, out elements);
Thread oc = new Thread(() => Pullout_contents(in_file, out elements));
oc.Start();
oc.Join();//necessary

//Show_contents(ref output_file, elements);
Thread sc = new Thread(() => Show_contents(ref output_file, elements));
sc.Start();
sc.Join();//not necessary

//Pushin_contents(out_file, elements);
Thread ic = new Thread(() => Pushin_contents(out_file, elements));
ic.Start();
ic.Join();// not necessary

if (input_file != null)
{
input_file.Close();
output_file.Close();
}
Console.ReadLine();
}
}
}

#cplusplus #csharp #tutorial