如何在C#中使用线程池? [英] how use threadpool in c#?
问题描述
我对线程感到很困惑.我有聊天应用程序,我使用了线程.现在的问题是100%的资源专用于此应用程序.
我使用了线程池.在这里
Hi,
I am very mess up with threads. I have chat application, i used thread. Now issue is that 100% resource are devoted to this application.
I used threadpool. here
ManualResetEvent eventX = new ManualResetEvent(false);
当Maxlength为100时,
when Maxlength is 100 then
lock (HashCount)
{
if (!HashCount.ContainsKey(Thread.CurrentThread.GetHashCode()))
HashCount.Add(Thread.CurrentThread.GetHashCode(), 0);
HashCount[Thread.CurrentThread.GetHashCode()] = ((int)HashCount[Thread.CurrentThread.GetHashCode()]) + 1;
}
我的逻辑读取聊天信息保存到数据库中
My Logic read chat info save into database
//// of variables accessible across multiple threads.
Interlocked.Increment(ref iCount);
if (iCount == iMaxCount)
{
eventX.Set();
}
我用上面的代码.当eventX.set()
触发时,关闭我的应用程序.我希望在达到极限后,应重置所有线程,并且不应关闭应用程序.它会一直运行直到没有停止射击.
请告诉我如何使我的应用程序最佳.
谢谢
Seema
I use above code. When eventX.set()
fired it close my application. I want after reaching limit all thread should be reset and application should not be closed. It keep run till not Stop fired.
Please tell me how could I make my application best.
Thanks
Seema
推荐答案
在这里我粘贴我正在使用的代码...请帮助我或建议我如何使我的UI应用程序更好并提高性能...
Hi,
Here I paste my Code that I am using...Please help me or suggest me how can I make my UI application better and increase performance...
//Button Start will start Server Connection
private void btnStart_Click(object sender, EventArgs e)
{
try
{
btnStart.Enabled = false;
btnStart.BackColor = System.Drawing.Color.DimGray;
btnClose.Enabled = true;
btnClose.BackColor = System.Drawing.Color.LightBlue;
t = new Thread(new ThreadStart(StartListner));
shutdown = false;
t.Start();
//Pause
Thread.Sleep(500);
//t.Join();
}
catch { }
}
//Start Listener
private void StartListner()
{
////clsServerClient obj = new clsServerClient();
//TcpListener
serverSocket = new TcpListener(Port);
clientSocket = default(TcpClient);
try
{
int counter = 0;
if(!shutdown)
{
serverSocket.Start();
SetText("Server Started !!!");
}
while (!shutdown)
{
counter += 1;
Thread.Sleep(100);
clientSocket = serverSocket.AcceptTcpClient();
startClient(clientSocket, Convert.ToString(counter));
}
//clientSocket.GetStream().Close();
clientSocket.Close();
serverSocket.Stop();
SetText("Exit!!!");
//Console.WriteLine(" >> " + "exit");
//Console.ReadLine();
}
catch { }
finally
{
}
}
// Here WE Will connected Client
public void startClient(TcpClient inClientSocket, string clineNo)
{
//string strclientIp = inClientSocket.Client.RemoteEndPoint.ToString();
this.clientSocket = inClientSocket;
this.clNo = clineNo;
//SetInfoText("Total Connected Client");
for (int i = 0; i < ctThread1.Length; i++)
{
ctThread1[i] = new Thread(new ThreadStart(doChat));
ctThread1[i].Start();
}
}
private void doChat()
{
int requestCount = 0;
byte[] bytesFrom = new byte[10025];
string dataFromClient = null;
Thread.Sleep(100);
requestCount = 0;
while ((!shutdown ))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
//Pause
Thread.Sleep(500);
string str = mySubString(dataFromClient, 1, 12) + "(" + clientSocket.Client.RemoteEndPoint.ToString() + ")";
SetInfoText(str);
SetText(Environment.NewLine+"Received !!!!"+Environment.NewLine+ dataFromClient);
//Delegate USe to Save Data
Savedt delSave = new Savedt(saveData);
delSave(dataFromClient);
//Thread.CurrentThread.Abort();
Thread.Sleep(2000);
clientSocket.Client.Close();
//Thread.CurrentThread.Join();
networkStream.Flush();
//Thread.Sleep(100); NOT WORKING
for (int i = 0; i < ctThread1.Length; i++)
{
ctThread1[i].Join();
}
}
catch (Exception ex)
{
Console.WriteLine(" >> " + ex.ToString());
}
}
}
这篇关于如何在C#中使用线程池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!