1

Необходимо в разных поток вызывать метод GetInfo и записать временные результаты в файл, но при его работе обрабатывается только пара файлов,одни и те же по нескольку раз даже бывает,как можно решить данную проблему?

 public static void GetInfo(object parametrs)
                {
                    object[] box = (object[])parametrs;
                    string fileName = (string)box[0];
                    Dictionary<string, long> result = new Dictionary<string, long>();
                    StreamReader reader;
                    string str;
                    var array = new String[2];
                       reader = File.OpenText(fileName);
                        while ((str = reader.ReadLine()) != null)
                        {
                            array = str.Split(',');
                            array[0] = array[0].ToLower();
                        try
                        {
                            result.Add(array[0], Convert.ToInt64(array[1]));//error
                        }
                        catch (ArgumentException)
                        {
                            long temp;
                            temp = Convert.ToInt64(result[array[0]]);
                            temp += Convert.ToInt64(array[1]);
                            result[array[0]] = temp;

                        }
                        catch (Exception)
                        {
                            return;
                        }

                            Console.WriteLine(fileName);
                        }
                    reader.Close();
                    //var a = new object();
                    lock (box[1])
                    {
                        var output = new FileStream("output.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                        var writer = new StreamWriter(output, Encoding.GetEncoding(1251));//utf-8
                        foreach (var item in result)
                        {
                            writer.WriteLine(item.Key + "," + item.Value);
                        }

                        writer.Close();
                    }
                    Console.WriteLine("-------------------");

                }

     public static void Main(string[] args)
            {
                string patternForName = "[a-z]", patternForPopulation="\\b"/*,str1="1 23  32"*/;
                Regex regexName = new Regex(patternForName);
                Regex regexPopulation = new Regex(patternForPopulation);
                var array = new String[2];
                Random rand=new Random();
                int N = rand.Next(1, 10);
                Thread[] threads = new Thread[N];

                Dictionary<string, long> result = new Dictionary<string, long>();
                if (args.Length != 2)
                    return;
                var input_mode = args[0];
                var input_address=args[1];
                var a = new object();
                object[] box = new object[2] ;
                if (input_mode == "filestream")
                {
                    var directory = new DirectoryInfo(@input_address);
                    if (directory.Exists)//выносить в функцию
                    {
                        FileInfo[] files = directory.GetFiles("*.txt",SearchOption.AllDirectories);
                        Console.WriteLine("file.length={0}",files.Length);

                        if (N < files.Length)
                        {
                            N = files.Length;
                            threads = new Thread[N];
                        }
                        for (int i = 0; i < files.Length; i++)
                        {
                            if (i >= N)
                            {
                                int j = 0;
                                while (true)
                                {
                                    if (threads[j].IsAlive == false)
                                    {
                                        box[0] = files[i].FullName;
                                        box[1] = a;
                 threads[j] = new Thread(new ParameterizedThreadStart(GetInfo));
                                        threads[j].Start(box);
                                        break;
                                    }
                                    if (j == N - 1)
                                        j =0;
                                }
                                continue;
                            }
                            box[0] = files[i].FullName;
                            box[1] = a;
               threads[i] = new Thread(new ParameterizedThreadStart (GetInfo));
                            threads[i].Start(box);
                        }


                    }

                    else
                        return;
                }
                if (input_mode == "http")
                {
                    var file = new FileStream(args[1],FileMode.Open,FileAccess.Read);
                    var reader = new StreamReader(file, Encoding.GetEncoding(1251));//utf-8
                    string row;
                    using (var client = new WebClient())
                    {
                            while ((row=reader.ReadLine())!=null)
                            {
                                client.DownloadFile(@row, @"D:\Internship\result.txt");
                                GetInfo(@"D:\Internship\result.txt"/*,result*/);
                            }



                    }

                }


                while (true)
                {
                    int notWorkThreads = 0;
                    for (int i = 0; i <threads.Length; i++)
                    {
                        if (threads[i]==null || threads[i].IsAlive == false)
                            notWorkThreads++;
                    }
                    if (notWorkThreads == N)
                        break;
                }

                    Console.ReadKey();
            }
        }

    }
1

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.