如何在窗口应用程序中放置线程 [英] How to dispose threads in window application

查看:71
本文介绍了如何在窗口应用程序中放置线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经创建了聊天应用程序.

我现在在哪里使用线程,子线程正在增加,这是一个问题.
这会导致CPU性能下降.

请告诉我一段时间后如何释放内存或资源,以提高CPU性能.

Hi,

I have created Chat application.

Where I am working with threads now I have an issue with the child threads that are increasing.
It causes CPU performance to slow down.

Please tell how can I release memory or resources after some time so that I can increase CPU performance.

public void startClient(TcpClient inClientSocket, string clineNo)
{
   //string strclientIp = inClientSocket.Client.RemoteEndPoint.ToString();
   this.clientSocket = inClientSocket;
   this.clNo = clineNo;

   //t.Join(2000);
   Thread ctThread = new Thread(doChat);
   ctThread.Start();
}
private void doChat()
{
   while ((!shutdown ))
   {
      try
      {
         requestCount = requestCount + 1;
         NetworkStream networkStream = clientSocket.GetStream();
         networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
         Thread.Sleep(2000); 
         delSave(dataFromClient);
         clientSocket.Client.Close();
         networkStream.Flush();
      } catch{}
   }
}


请告诉我该在哪里释放该线程.
请帮帮我!
Seema


Please tell where should I release this thread.
Please help me!
Seema

推荐答案

您可能没有正确管理线程.如果每个连接的客户端都有其自己的线程,请确保在断开连接时正在中止线程.您还可以使用线程池来控制并发线程数,以进一步解决此问题.这意味着您的聊天应用程序在任何给定时间仅允许N个客户端连接,但这应该可以.
You probably aren''t managing the threads correctly. If each attached client has its own thread, make sure you''re aborting the thread when they disconnect. You can also use a thread pool to control the number of concurrent threads to further manage this problem. This means that your chat app will only allow N-number of clients to connect at any given time, but that should be okay.


为什么不尝试使用线程池,这会有所帮助您解决问题.我认为这可以解决您的问题.
Why not try using a thread-pool, this will help you manage the problem. I think this will solve your problem.


是的,您可以使用abort函数,也可以关注同时运行的并发线程数.
Yes you can use the abort function, also concern the number of concurrent threads running at same time.


这篇关于如何在窗口应用程序中放置线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆