Search code examples
c#winformssocketstcp

Threadpool.cs not found in win form


i want write a chatroom with c# and Tcp but i have Threadpool.cs not found error.

this is my error

how to can i fix this ? this is my code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Client
{
    public partial class Client : Form
    {
        Socket socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        public Client()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            IPEndPoint ipServer = new IPEndPoint(IPAddress.Parse(txtIp.Text), int.Parse(txtPort.Text));
            socClient.Connect(ipServer);
        }
    }
}
 

Solution

  • In Your Server Side Form1

    you tried to update your list box from another thread so you faced this issue

    on other to solve it you have to change your code from this :

    void Accept(IAsyncResult res)
    {
        socClient = socServer.EndAccept(res);
        listMessage.Items.Add("Client Connected...");
    }
    

    to this

            private delegate void UpdateListDelegate();
            void Accept(IAsyncResult res)
            {
                socClient = socServer.EndAccept(res);
                listMessage.Invoke(new UpdateListDelegate(() =>
                {
                    listMessage.Items.Add("Client Connected...");
                }));
            }
    

    so the main key is to Invoke your element whenever you are not working with main UI Thread.