Reading text file data through multithreading in C # .net

I am reading a text file consisting of 6 columns. Among 6 columns, every 3 columns show one object information. I want to access these columns in parallel through multithreading. Like three columns for one object, a total of 2 streams were created, except for the main stream. The text file is as follows: Text file Image
I tried this, but it is difficult for me to transfer data from the main stream to other streams. The error occurs with the string variable "part". (the variable part does not exist in the current context)

I want to do multithreading for tag1 and tag2.

I am sharing a block of my code, please suggest me where I am going wrong Since I am new to multi-threaded programming.

namespace MultiTag_Simulation_ConsoleApp
{
    class Program
    {    
      static void Main(string[] args)
       {
          string line;
          string[] part;

        StreamReader File = new StreamReader("2Tags_Points.txt");

        while((line = File.ReadLine()) !=null)
        {
            part = line.Split('\t');
            Thread TAG1 = new Thread(new ThreadStart(Tag1));
            TAG1.Start();
        }
    }

    void Tag1()
    {
        double w, x;
        w = Convert.ToDouble(part[1]);
        x = Convert.ToDouble(part[2]);

        Console.WriteLine("Tag1 x:" + w + "\t" + "Tag1 y:" + x);
        Console.ReadKey();
    }
  }
}
+4
2

, , . . , "part" .

 static string [] part 
+1

, , . , , , , . , , , , .

, . , , , .

namespace MultiTag_Simulation_ConsoleApp
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;

    internal static class Program
    {
        internal static void Main()
        {
            Parallel.ForEach(
                File.ReadLines("2Tags_Points.txt").Select(line => line.Split('\t')),
                parts =>
                    {
                        var w = Convert.ToDouble(parts[1]);
                        var x = Convert.ToDouble(parts[2]);
                        Console.WriteLine("Tag1 x:" + w + "\t" + "Tag1 y:" + x);

                        var y = Convert.ToDouble(parts[4]);
                        var z = Convert.ToDouble(parts[5]);
                        Console.WriteLine("Tag2 x:" + y + "\t" + "Tag2 y:" + z);
                    });
        }
    }
}
0

Source: https://habr.com/ru/post/1690671/


All Articles