Beispiel #1
0
 public void FirstThreadMain(object obj)
 {
     _post = (PostToSecondWT)obj;
     ConsoleHelper.WriteToConsole("1 поток", "Начинаю работу.Готовлю данные для передачи.");
     _sendMessage = new BitArray(56);
     for (int i = 0; i < 56; i++)
     {
         if (i % 2 == 0)
         {
             _sendMessage[i] = true;
         }
         else
         {
             _sendMessage[i] = false;
         }
     }
     _post(_sendMessage);
     //_sendSemaphore.Release();
     ConsoleHelper.WriteToConsole("1 поток", "Данные переданы");
     ConsoleHelper.WriteToConsole("1 поток", "Жду передачи данных");
     //_receiveSemaphore.WaitOne();
     ConsoleHelper.WriteToConsole("1 поток", "Данные получены.");
     ConsoleHelper.WriteToConsoleArray("1 поток", _receivedMessage);
     ConsoleHelper.WriteToConsole("1 поток", "Завершаю работу.");
 }
Beispiel #2
0
        static void Main(string[] args)
        {
            ConsoleHelper.WriteToConsole("Главный поток", "");
            Semaphore      firstReceiveSemaphore  = new Semaphore(0, 1);
            Semaphore      secondReceiveSemaphore = new Semaphore(0, 1);
            FirstThread    firstThread            = new FirstThread(ref secondReceiveSemaphore, ref firstReceiveSemaphore);
            SecondThread   secondThread           = new SecondThread(ref firstReceiveSemaphore, ref secondReceiveSemaphore);
            Thread         threadFirst            = new Thread(new ParameterizedThreadStart(firstThread.FirstThreadMain));
            Thread         threadSecond           = new Thread(new ParameterizedThreadStart(secondThread.SecondThreadMain));
            PostToFirstWT  postToFirstWt          = new PostToFirstWT(firstThread.ReceiveData);
            PostToSecondWT postToSecondWt         = new PostToSecondWT(secondThread.ReceiveData);

            threadFirst.Start(postToSecondWt);
            threadSecond.Start(postToFirstWt);
            Console.ReadLine();
        }
Beispiel #3
0
        public void FirstThreadMain(object obj)
        {
            _post = (PostToSecondWT)obj;
            ConsoleHelper.WriteToConsole("1 поток", "Начинаю работу. Готовлю данные для передачи.");

            //_sendMessage = "Привет! Я студент РУТ (МИИТ)";
            var bits = new BitArray(_encoding.GetBytes(_sendMessage));

            bool[] values = new bool[bits.Count];
            for (int m = 0; m < bits.Count; m++)
            {
                values[m] = bits[m];
            }

            int j = 0;

            result = values.GroupBy(s => j++ / Utils.FrameLength).Select(g => g.ToArray()).ToArray();

            Frame frame;

            if (result[i].Length == Utils.FrameLength)
            {
                frame = new Frame(i, new BitArray(result[i]), Utils.CheckSum(result[i]), result[i].Length);
            }
            else
            {
                frame = new Frame(i, AddedData(), Utils.CheckSum(result[i]), result[i].Length);
            }


            _post(new BitArray(Utils.SerializeObject(frame)));
            _sendSemaphore.Release();

            ConsoleHelper.WriteToConsole("1 поток", $"Передан {i} кадр");
            ConsoleHelper.WriteToConsole("1 поток", "Жду результата");
            i += 1;
            _receiveSemaphore.WaitOne();
            SetData();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            ConsoleHelper.WriteToConsole("Главный поток", "Введите Ваше сообщение...");
            var      data     = Console.ReadLine();
            Encoding encoding = Encoding.UTF8;

            Semaphore firstReceiveSemaphore  = new Semaphore(0, 1);
            Semaphore secondReceiveSemaphore = new Semaphore(0, 1);

            FirstThread  firstThread  = new FirstThread(ref secondReceiveSemaphore, ref firstReceiveSemaphore, encoding, data);
            SecondThread secondThread = new SecondThread(ref firstReceiveSemaphore, ref secondReceiveSemaphore, encoding);

            Thread threadFirst  = new Thread(new ParameterizedThreadStart(firstThread.FirstThreadMain));
            Thread threadSecond = new Thread(new ParameterizedThreadStart(secondThread.SecondThreadMain));

            PostToFirstWT  postToFirstWt  = new PostToFirstWT(firstThread.ReceiveData);
            PostToSecondWT postToSecondWt = new PostToSecondWT(secondThread.ReceiveData);

            threadFirst.Start(postToSecondWt);
            threadSecond.Start(postToFirstWt);

            Console.ReadLine();
        }