Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I have created sample application in which i have created 3 threads which will be responsible to send some data to Port.. i have created two functions one is StartSending1 and StartSending2 but if I use any one of these still my CPU goes upto 70 to 80%

Please suggest what should be better way to minimize load on CPU without Thread.Sleep()

Please check sample

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;

namespace ConsoleApplication1
{
    class Program
    {
        
        static System.Threading.Thread sender;
        static DataSender dataSender;
       
        static void Main(string[] args)
        {            
            dataSender = new DataSender();
            var dataSender2 = new DataSender();
            var dataSender3 = new DataSender();
            var dataSender4 = new DataSender();

            sender = new System.Threading.Thread(dataSender.StartSending2);
            sender.IsBackground = true;
            sender.Start();

            sender = new System.Threading.Thread(dataSender2.StartSending2);
            sender.IsBackground = true;
            sender.Start();

            sender = new System.Threading.Thread(dataSender3.StartSending2);
            sender.IsBackground = true;
            sender.Start();

            Console.ReadKey();
        }       
    }

    class SnderEvrgs : EventArgs
    {
        public SnderEvrgs()
        {
        }
    }

    class DataSender
    {
        private static string IP = "127.0.0.1";
        public static int Port = 20100;
        static bool issending = true; 
        public static event EventHandler<SnderEvrgs> DataSent;

        public void StartSending1()
        {
            while (issending)
            {
                System.Net.Sockets.UdpClient _sockMain = new System.Net.Sockets.UdpClient(IP, Port);
                byte[] arr_bData = new byte[] { 1, 1, 1, 1, 1 };
                Console.WriteLine(DateTime.Now);

                for (int i = 0; i < 100000000; i++)
                {
                }                            
            }
        }

        static AutoResetEvent abc = new AutoResetEvent(false);
        Thread newthread;
        
        public void StartSending2()
        {
            while (issending)
            {
                newthread = new Thread(sendData);
                newthread.IsBackground = true;
                newthread.Priority = ThreadPriority.Lowest;
                newthread.Start();
                abc.WaitOne();
            }
        }

        object abca = new object();

        [MethodImplAttribute(MethodImplOptions.Synchronized)]
        public void sendData()
        {
            lock (abca)
            {
                System.Net.Sockets.UdpClient _sockMain = new System.Net.Sockets.UdpClient(IP, Port);
                byte[] arr_bData = new byte[] { 1, 1, 1, 1, 1 };
                Console.WriteLine(DateTime.Now);
                if (_sockMain.Send(arr_bData, arr_bData.Length) == arr_bData.Length) ;
                {
                    for (int i = 0; i < 1000000000; i++)
                    {
                    }

                    abc.Set();
                }
            }
        }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 21-Sep-14 13:32pm    
The question is: why?
—SA

1 solution

Why?
Have you looked at what you are doing there?
You create a new thread to handle the StartSending2 method, and that thread sits in a loop creating new threads and kicking them off. The new threads write to the console, then sit in a loop doing a total of nothing but waste CPU cycles.

And you are surprised that you CPU usage is 70~80%?
I'm surprised it's only 70~80%...
 
Share this answer
 
Comments
OriginalGriff 21-Sep-14 11:02am    
Because it does the same damn thing, with one less thread? :laugh:
OriginalGriff 21-Sep-14 11:32am    
"Why you are considering code inside startSending1 or StartSending2 Methods"
Because that is all you have given us to work with.


"Code can be anything complex code. But i need solution how to handle this complex code in way that it should not consume CPU."
And there is no "generic solution" to reducing CPU usage: it depends on the task, and other factors. You can't give a solution based on "sample" code if the sample doesn't do anything like the "real" code - any more than I can predict the fuel consumption of your car and recommend ways to improve it without knowing what car it is, what you use it for, and how you drive it! :laugh:

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