none
threadpool issue

    Question

  • I am using threadpool for my tcp server application. here client is barcode reader.

    I have used a list of type tcp client for storing accepted tcp clients.

    i am iterating through all the elements of tcpclient.  my code is as follows.

     foreach (TcpClient item in lstclients)
                        {
                        IPEndPoint remoteIpEndPoint = item.Client.RemoteEndPoint as IPEndPoint;
                     
                        Task.Factory.StartNew(() => ThreadProc(item, remoteIpEndPoint));

    }

    and the threadpool method is as follows

       private void ThreadProc(object obj,IPEndPoint IP)
            {
                var client = (TcpClient)obj;
                NetworkStream stream1 = client.GetStream();
                // Do yourclient work here
                Byte[] bytes = new Byte[1024];
                String data1 = null;
                while (true)
                {
                   
                    data1 = null;
                    
                    int i;
                    i = stream1.Read(bytes, 0, bytes.Length);
                    data1 = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                  
                    scannerip = IP.Address.ToString();

                    this.InvokeEx(() => { AppendText(data1, scannerip); });
                 
                  
                }
            }

    foreach loop iterating through all clients. but in thread loop it is showing for only few clients. 

    can any one solve this issue

    Thursday, March 30, 2017 11:11 AM

Answers

  • Hi nayakSudhakar,

    Thank you for posting here.

    For your question, you create all the task with clients. But you could not make sure that all the tasks run. You could use Task.IsCompleted Property to check that whether the task has completed.

    Maybe some of the tasks are wait to run.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, March 31, 2017 9:20 AM
    Moderator

All replies

  • Hi nayakSudhakar,

    Thank you for posting here.

    For your question, you create all the task with clients. But you could not make sure that all the tasks run. You could use Task.IsCompleted Property to check that whether the task has completed.

    Maybe some of the tasks are wait to run.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, March 31, 2017 9:20 AM
    Moderator
  • I my code i am assigning a variable to the task and checking. in my case cleint(barcode scanner is continuously sending the data. I dont know how to handle it.

                    

     private void ThreadProc(object obj,IPEndPoint IP)
            {
                var client = (TcpClient)obj;
                NetworkStream stream1 = client.GetStream();
                // Do yourclient work here
                Byte[] bytes = new Byte[1024];
                String data1 = null;
                while (true)
                {
                    // Console.WriteLine("Connected! to " + client.Client.RemoteEndPoint);
                    data1 = null;

                    int i;
                    i = stream1.Read(bytes, 0, bytes.Length);
                    data1 = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    //  scannerip = client.Client.RemoteEndPoint as IPEndPoint;
                    scannerip = IP.Address.ToString();

                    this.InvokeEx(() => { AppendText(data1, scannerip); });
                  //  listBoxLog.Log(Level.Debug, "Value scanned from "+scannerip+" for calibration is " + data1);
                   if( tsk.IsCompleted)
                    {
                        return;
                    }

    }

    }

    Friday, March 31, 2017 9:54 AM
  •  while (!(tsk.IsCompleted))
                {



                    var client = (TcpClient)obj;
                    NetworkStream stream1 = client.GetStream();
                    // Do yourclient work here
                    Byte[] bytes = new Byte[1024];
                    String data1 = null;
                    while (true)
                    {
                        // Console.WriteLine("Connected! to " + client.Client.RemoteEndPoint);
                        data1 = null;

                        int i;
                        i = stream1.Read(bytes, 0, bytes.Length);
                        data1 = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        //  scannerip = client.Client.RemoteEndPoint as IPEndPoint;
                        scannerip = IP.Address.ToString();

                        this.InvokeEx(() => { AppendText(data1, scannerip); });
                        //  listBoxLog.Log(Level.Debug, "Value scanned from "+scannerip+" for calibration is " + data1);

                        //AppendText(data1);
                        // Thread.Sleep(1000);
                    }
                }
            }

    this way i am trying to check whether the task is completed or not.

    but barcode reader is sending data continuously.  

    Friday, March 31, 2017 10:10 AM
  • Dear Sir,

    I my application the task has to run continuously. Because the barcode scanner is sending data continuously. but in my current application the function will excute for a single task more time. 

    Friday, March 31, 2017 11:01 AM