Esempio n. 1
0
 public static void SendMessagesToServer(IEnumerable <string> messages)
 {
     Helper.TraceLine("Started SendMessagesToClient Task");
     using (var client = new BlockClient(Client.Helper.GetLocalIpv4(), 11000))
     {
         client.Connect();
         foreach (var message in messages)
         {
             string response;
             client.Send(message, out response);
             Thread.Sleep(500);
             Helper.TraceLine("SendMessagesToClient(...{0}{1} > {2}", Environment.NewLine, message, response);
         }
         client.Close();
     }
     Helper.TraceLine("Finished SendMessagesToClient Task");
 }
Esempio n. 2
0
        protected Task LongRunningTask(CancellationToken issResetToken)
        {
            CancellationToken manualCancelation = LongTaskCancelation.Token;

            try
            {
                Di.Trace.Write("Long running task started");

                using (var client = new BlockClient(Client.Helper.GetLocalIpv4(), 11000))
                {
                    client.Connect();
                    for (int i = 0; i < 10000; i++)
                    {
                        if (issResetToken.IsCancellationRequested || manualCancelation.IsCancellationRequested)
                        {
                            break;
                        }

                        string command = string.Format("Sending message {0}", i);
                        string response;

                        if (client.Send(command, out response))
                        {
                            Di.Trace.WriteLine("Message sent to server");
                            Di.Trace.WriteLine(string.Format("{0} > {1}", command, response));
                        }
                        else
                        {
                            Di.Trace.WriteLine("Can't send message to server. Probably error in communication");
                            Di.Trace.WriteLine("Ending long running task.");
                            break;
                        }
                    }
                    client.Close();
                }
            }
            catch (Exception e)
            {
                Di.Trace.WriteLine("Error in long running task");
                Di.Trace.WriteLine(e.Message);
                Di.Trace.WriteLine(e.StackTrace);
            }
            return(Task.FromResult(false));
        }