Click here to Skip to main content
15,917,061 members
Please Sign up or sign in to vote.
2.50/5 (4 votes)
See more:
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);


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();
}



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
Posted
Updated 22-Jul-11 9:29am
v3
Comments
RaisKazi 22-Jul-11 14:57pm    
Format changes.
#realJSOP 22-Jul-11 15:30pm    
You haven't shown ANY code that can help us to help you.
Sergey Alexandrovich Kryukov 22-Jul-11 18:03pm    
For example, if you created eventX, where is the code calling eventX.WaitOne()? Where is the interlocked use of iCount in any other thread? Where is eventX.Set() called? If this is in manual mode, where eventX.Reset() called? Don't you think we cannot help you fix things while blindfolded?
--SA

1 solution

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...

MSIL
//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());
                }
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900