Beispiel #1
0
        private void BW_NPipe_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            /*
             * var server = new NamedPipeServer("npstest");
             * server.StartListen();
             *
             * while (work)
             * {
             *  sw.Restart();
             *  var _in = server.AwaitSingleMessageAsync<char[]>().Result;
             *  string s = new string(_in.Message);
             *  Console.WriteLine("<<" + s);
             *  Task.Run(async () =>
             *  {
             *      if(await is_pinged()) responce = GetFromNet();
             *          else
             *          {
             *              responce = "NOT PINGED";
             *          }
             *      ;
             *      await server.SendMessageToAllAsync(responce+">>" + s);
             *  }).Wait(1000);
             *  sw.Stop();
             *  if(sw.ElapsedMilliseconds < 1000) Thread.Sleep((int)(1000-sw.ElapsedMilliseconds));
             * }
             */
            Console.WriteLine("CALL" + work + "|" + (server == null));

            bool   work_now = true;
            string s        = "";
            int    trys     = 99;

            while (server == null)
            {
                NewServer();
                trys--;
                Task.Delay(1000);
                if (trys == 0)
                {
                    throw new Exception("BAD DAY");
                }
            }
            //else
            if (server != null)
            {
                var _1 = server.AwaitSingleMessageAsync <char[]>(ct);
                _1.ConfigureAwait(false);

                var _3 = _1.GetAwaiter();
                NamedPipeMessage <char[]> _4 = null;
                try
                {
                    _4 = _3.GetResult();
                }
                catch (OperationCanceledException OCE)
                {
                    Console.WriteLine(OCE);
                    work_now = false;
                }
                catch (NullReferenceException NRE)
                {
                    Console.WriteLine(NRE);
                    work_now = false;
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    throw;
                }

                if (work_now && work)
                {
                    Task.Run(async() =>
                    {
                        if (_4 == null)
                        {
                            return;
                        }

                        s = new string(_4.Message);

                        //fastColoredTextBox1.AppendText("\n" + s + "<<");
                        if (await is_pinged().ConfigureAwait(false))
                        {
                            responce = GetFromNet();
                        }
                        else
                        {
                            responce = "NOT PINGED";
                        }

                        await server.SendMessageToAllAsync("\n" + responce + "|>>" + s, ct)
                        .ConfigureAwait(false);
                    }, ct).Wait(10000, ct);
                }
            }
        }