Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.IO;
  7. using System.IO.Pipes;
  8.  
  9. namespace MultiThread
  10. {
  11.     class Program
  12.     {
  13.         static public object LockObj = new object();
  14.  
  15.         static public Stream outStream;
  16.         static public Stream inStream;
  17.  
  18.         public static AnonymousPipeServerStream pipeServer;
  19.         public static AnonymousPipeClientStream pipeClient;
  20.  
  21.         static public string pipeHandle;
  22.         static int mode = 0;
  23.         static public int i = 0;
  24.         public static float sredn = 0, dlina = 0;
  25.         static bool stop = false;
  26.  
  27.         public static void Second()
  28.         {
  29.             pipeClient = new AnonymousPipeClientStream
  30. (PipeDirection.In, pipeHandle);
  31.  
  32.             string buffer = "";
  33.             StreamReader binReader = new StreamReader(pipeClient);
  34.             while (!stop)
  35.             {
  36.                 buffer = binReader.ReadLine();
  37.  
  38.                 lock(LockObj)
  39.                 {
  40.                     int tmp = mode;
  41.                     Console.WriteLine("Message read ({0}): {1}",
  42.     DateTime.Now, buffer);
  43.                     if (tmp == 1)
  44.                     {
  45.                         Console.WriteLine("выбрано 1");
  46.                         string[] msgRcv = buffer.Split(':');
  47.                         int n;
  48.                         int counter = 0;
  49.                         for (int j = 0; j < msgRcv.Length; j++)
  50.                         {
  51.                             if (Int32.TryParse(msgRcv[j], out n))
  52.                             {
  53.                                 counter++;
  54.                             }
  55.                         }
  56.  
  57.                         Console.WriteLine("Числовых строк: {0}", counter);
  58.                     }
  59.                     else if (tmp == 2)
  60.                     {
  61.                         int sum = 0;
  62.  
  63.                         string[] msgRcv = buffer.Split(':');
  64.  
  65.                         Console.WriteLine("Обработка числового массива");
  66.                         for (int j = 0; j < msgRcv.Length; j++)
  67.                         {
  68.                             if (j % 2 != 0 && Convert.ToInt32(msgRcv[j]) > 0)
  69.                             {
  70.                                 sum++;
  71.                             }
  72.                         }
  73.                         Console.WriteLine("sum = {0}", sum);
  74.                     }
  75.                     else
  76.                     {
  77.                         Console.WriteLine("Unknown command!");
  78.                     }
  79.                 }
  80.             }
  81.             binReader.Close();
  82.         }
  83.  
  84.         public static void Main()
  85.         {
  86.             Thread th2 = new Thread(() => Second());
  87.             pipeServer = new AnonymousPipeServerStream
  88. (PipeDirection.Out);
  89.             outStream = pipeServer;
  90.             pipeHandle = pipeServer.GetClientHandleAsString();
  91.             th2.Start();
  92.             StreamWriter writer = new StreamWriter(outStream);
  93.             writer.AutoFlush = true;
  94.  
  95.             while (!stop)
  96.             {
  97.                 Console.WriteLine("1 - подсчет количества слов из цифр (строка разделяется на слова с помощью двоеточия)");
  98.                 Console.WriteLine("2 - подсчет числа положительных элементов с четными индексами");
  99.                 int com;
  100.  
  101.                 if (Int32.TryParse(Console.ReadLine(), out com))
  102.                 {
  103.                     if (com != 1 && com != 2) { com = 3; }
  104.                 }
  105.                 else
  106.                 {
  107.                     Environment.Exit(0);
  108.                 }
  109.  
  110.                 lock (LockObj)
  111.                 {
  112.                     mode = com;
  113.                     switch (com)
  114.                     {
  115.                         case 1:
  116.                             Console.WriteLine("Введите строку (разделение на двоеточия)");
  117.                             string stroka = Console.ReadLine();
  118.  
  119.                             writer.WriteLine(stroka);
  120.                             break;
  121.                         case 2:
  122.                             // 2. Подсчет количества неотрицательных элементов в массиве
  123.                             Console.WriteLine("Введите размер числового массива: ");
  124.                             int arraySize = Convert.ToInt32(Console.ReadLine());
  125.                             string stroka2 = "";
  126.  
  127.                             for (int i = 0; i < arraySize; i++)
  128.                             {
  129.                                 Console.WriteLine("Введите элемент массива под номером {0}: ", i + 1);
  130.                                 stroka2 += Convert.ToString(Console.ReadLine());
  131.                                 if (i < arraySize - 1)
  132.                                 {
  133.                                     stroka2 += ":";
  134.                                 }
  135.                             }
  136.  
  137.                             writer.WriteLine(stroka2);
  138.                             break;
  139.                         case 3:
  140.                             stop = true;
  141.                             break;
  142.                     }
  143.                 }
  144.             }
  145.             writer.Close();
  146.  
  147.             th2.Join();
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement