Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

TaskFactory problem

$
0
0

Hello.

I have code.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using YP.ToolKit.Protocols.ModBus.Transport;

namespace SmokeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //var task = new Task<IEnumerable<int>>(AsyncSmokeTest.SendReceiveRequests(new InformationClass("127.0.0.1", 1300, new byte[] { 12, 12, 12, 12, 12, 12, 12, 12 })));
            //var task = new Task<IEnumerable<int>>(n => AsyncSmokeTest.SendReceiveRequests((InformationClass)n), new InformationClass("127.0.0.1", 1300, new byte[]{12,12,12,12,12}));
            var data = new byte[] { 0, 2, 0, 0, 0, 6, 0, 4, 0, 2, 0, 0x1C };
            var watch = new Stopwatch();
            watch.Start();
                TaskFactory parent = new TaskFactory();

                var tasks = new[]
                {
                    parent.StartNew(
                        () => SendReceiveRequests(new InformationClass("010.024.135.208", 4444, data))),
                    parent.StartNew(
                        () => SendReceiveRequests(new InformationClass("010.024.135.209", 4444, data))),
                    parent.StartNew(
                        () => SendReceiveRequests(new InformationClass("010.024.135.210", 4444, data))),
                    parent.StartNew(
                        () => SendReceiveRequests(new InformationClass("010.024.135.211", 4444, data))),
                    parent.StartNew(
                        () => SendReceiveRequests(new InformationClass("010.024.135.212", 4444, data))),
                    parent.StartNew(
                        () => SendReceiveRequests(new InformationClass("010.024.135.213", 4444, data))),
                    parent.StartNew(
                        () => SendReceiveRequests(new InformationClass("010.024.135.214", 4444, data))),
                    parent.StartNew(
                        () => SendReceiveRequests(new InformationClass("010.024.135.215", 4444, data))),
                    parent.StartNew(
                        () => SendReceiveRequests(new InformationClass("010.024.135.216", 4444, data))),
                    parent.StartNew(
                        () => SendReceiveRequests(new InformationClass("010.024.135.217", 4444, data)))
                };
            Task.WaitAll(tasks);
            Console.ReadLine();
            //mainTask.Start();
            //mainTask.Wait();
            //Task.WaitAll(tasks);
            watch.Stop();
            Console.WriteLine("{0}", watch.Elapsed);
        }

        public static IEnumerable<int> SendReceiveRequests(InformationClass information)
        {
            TcpClientTransport client = new TcpClientTransport(information._ipAddress, information._port);

            IAsyncResult asyncResult = client.BeginWrite(information._bufSend, 0, information._bufSend.Length, information.Enumerator.End(), null);
            yield return 1;

            client.EndWrite(information.Enumerator.DequeueAsyncResult());

            Console.WriteLine("Writing ended.");
            //Console.ReadLine();

            IAsyncResult asyncResultOfRead = client.BeginRead(information._bufReceive, 0, information._bufReceive.Length, information.Enumerator.End(), null);
            yield return 1;

            int size = client.EndRead(information.Enumerator.DequeueAsyncResult());
            byte[] data = new byte[size];
            for (int i = 0; i < size; i++)
            {
                Console.Write("{0}  ", information._bufReceive[i]);
            }
            Console.WriteLine();
            //for (int i = 0; i < size; i++)
            //{
            //    Console.Write("{0}  ", data[i]);
            //}
        }
    }
}

But my static method SendReceiveRequests doesn't called and program goes to Console.ReadLine().
Please, help me and tell why.

Sorry for my English.


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>