0

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);
        }
    }
}
 
  • you have the error in the screenshot. looks like you are accessing the ui components from a different thread. – EylM Aug 16 '22 at 15:41
  • 1
    Does this answer your question? [Solve a cross-threading Exception in WinForms](https://stackoverflow.com/questions/5868783/solve-a-cross-threading-exception-in-winforms) – AliSalehi Aug 16 '22 at 15:52
  • no when i use ``` socClient.Connect(ipServer); ``` i have this error. this not a ui |: @EylM – MahdiKarami Aug 16 '22 at 15:54
  • That screenshot is because you have "Just My Code" debugging unticked in the Visual Studio options. "threadpool.cs" is not relevant here, it's the cross-threading that's the problem – Charlieface Aug 16 '22 at 18:52

2 Answers2

1

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.

AliSalehi
  • 159
  • 1
  • 1
  • 13
0

Your problem is that you have a cross-thread call. You can hack around that by using Invoke. But I would strongly advise you to switch to async await, which will maintain the current context.

 public partial class Client : Form
 {
     Socket socClient;

     public Client()
     {
         InitializeComponent();
     }

     private async void btnConnect_Click(object sender, EventArgs e)
     {
         socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         IPEndPoint ipServer = new IPEndPoint(IPAddress.Parse(txtIp.Text), int.Parse(txtPort.Text));
         await socClient.ConnectAsync(ipServer);
     }
 }

I also suggest you think about whether you want to use raw sockets, as it's much easier to use TcpClient.

Charlieface
  • 52,284
  • 6
  • 19
  • 43