Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.IO;
- using System.IO.Pipes;
- namespace MultiThread
- {
- class Program
- {
- static public object LockObj = new object();
- static public Stream outStream;
- static public Stream inStream;
- public static AnonymousPipeServerStream pipeServer;
- public static AnonymousPipeClientStream pipeClient;
- static public string pipeHandle;
- static int mode = 0;
- static public int i = 0;
- public static float sredn = 0, dlina = 0;
- static bool stop = false;
- public static void Second()
- {
- pipeClient = new AnonymousPipeClientStream
- (PipeDirection.In, pipeHandle);
- string buffer = "";
- StreamReader binReader = new StreamReader(pipeClient);
- while (!stop)
- {
- buffer = binReader.ReadLine();
- lock(LockObj)
- {
- int tmp = mode;
- Console.WriteLine("Message read ({0}): {1}",
- DateTime.Now, buffer);
- if (tmp == 1)
- {
- Console.WriteLine("выбрано 1");
- string[] msgRcv = buffer.Split(':');
- int n;
- int counter = 0;
- for (int j = 0; j < msgRcv.Length; j++)
- {
- if (Int32.TryParse(msgRcv[j], out n))
- {
- counter++;
- }
- }
- Console.WriteLine("Числовых строк: {0}", counter);
- }
- else if (tmp == 2)
- {
- int sum = 0;
- string[] msgRcv = buffer.Split(':');
- Console.WriteLine("Обработка числового массива");
- for (int j = 0; j < msgRcv.Length; j++)
- {
- if (j % 2 != 0 && Convert.ToInt32(msgRcv[j]) > 0)
- {
- sum++;
- }
- }
- Console.WriteLine("sum = {0}", sum);
- }
- else
- {
- Console.WriteLine("Unknown command!");
- }
- }
- }
- binReader.Close();
- }
- public static void Main()
- {
- Thread th2 = new Thread(() => Second());
- pipeServer = new AnonymousPipeServerStream
- (PipeDirection.Out);
- outStream = pipeServer;
- pipeHandle = pipeServer.GetClientHandleAsString();
- th2.Start();
- StreamWriter writer = new StreamWriter(outStream);
- writer.AutoFlush = true;
- while (!stop)
- {
- Console.WriteLine("1 - подсчет количества слов из цифр (строка разделяется на слова с помощью двоеточия)");
- Console.WriteLine("2 - подсчет числа положительных элементов с четными индексами");
- int com;
- if (Int32.TryParse(Console.ReadLine(), out com))
- {
- if (com != 1 && com != 2) { com = 3; }
- }
- else
- {
- Environment.Exit(0);
- }
- lock (LockObj)
- {
- mode = com;
- switch (com)
- {
- case 1:
- Console.WriteLine("Введите строку (разделение на двоеточия)");
- string stroka = Console.ReadLine();
- writer.WriteLine(stroka);
- break;
- case 2:
- // 2. Подсчет количества неотрицательных элементов в массиве
- Console.WriteLine("Введите размер числового массива: ");
- int arraySize = Convert.ToInt32(Console.ReadLine());
- string stroka2 = "";
- for (int i = 0; i < arraySize; i++)
- {
- Console.WriteLine("Введите элемент массива под номером {0}: ", i + 1);
- stroka2 += Convert.ToString(Console.ReadLine());
- if (i < arraySize - 1)
- {
- stroka2 += ":";
- }
- }
- writer.WriteLine(stroka2);
- break;
- case 3:
- stop = true;
- break;
- }
- }
- }
- writer.Close();
- th2.Join();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement