Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to learn the ram usage of a service running on windows services with c # console application


What I have tried:

Timer timer = new Timer();
       public void Start()
       {
           startService("Start service");
           timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
           timer.Interval = 100000; //ınvertal ile sureyi belirtiyortuz
           timer.Enabled = true;
       }
       public void Stop()
       {
           // WriteToFile("Service Durduruldu " + DateTime.Now);
       }
       private void OnElapsedTime(object source, ElapsedEventArgs e)// zamanlayıcıyı baslatıyor
       {
           startService("Another entry at " + DateTime.Now);
       }
       public void startService(string content)
       {
           try
           {
               PerformanceCounter counter = new PerformanceCounter("Process", "% Processor Time", "ServiceName", true);
               ServiceController sc = new ServiceController();

               var ser = ConfigurationManager.AppSettings["ServiceName"];
               var name = ser.Split(',');
               string sname = "";
               foreach (var item in name)
               {
                   sname = item.Trim();
                   sc.ServiceName = sname;
                   if ((sc.Status.Equals(ServiceControllerStatus.Stopped)))
                   {
                       sc.Start();// Durdurulduysa servisi baslat

                       // Display the current service status.
                       Console.WriteLine("The started service status is now set to {0}.",
                                              sc.Status.ToString());

                   }
                   // sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 1, 0));

                   else
                   {
                       // Stop the service if its status is not set to "Stopped".

                       Console.WriteLine("Running the RKService service...");
                       //sc.Stop();

                   }

                   sc.Refresh();

               }
Posted
Updated 23-Mar-21 5:01am
Comments
Richard Deeming 23-Mar-21 10:54am    
REPOST
This is basically the same as your previous question:
How to stop a service running C # windows service controller according to the ram usage[^]
Member 14623865 24-Mar-21 2:27am    
yes ı know

1 solution

You can see the C# API that you can use to do this at:

Process.WorkingSet64 Property (System.Diagnostics) | Microsoft Docs[^]

That will provide you with the necessary calls you can do to get the amount of memory that is being consumed by a process.

However, to write a console app that interacts with the service is something that takes a lot more explaining.

This is Interprocess Communication and is a lot more involved: Interprocess Communications - Win32 apps | Microsoft Docs[^]

Here's what you'd have to do:

1) Create a method in your service that will return the memory details as JSON.
2) Use WCF (Windows Comunication Foundation -- Windows Communication Foundation - Wikipedia[^] ) to build the IPC into the Service -- this will call into the method in your service that gets the memory info.
3) Call the WCF from your console app to get the JSON
4) Display the returned JSON in your console app.

Here's a basic getting started sample for WCF: Tutorial: Host and run a basic Windows Communication Foundation service - WCF | Microsoft Docs[^]
 
Share this answer
 
v3
Comments
Member 14623865 24-Mar-21 2:35am    
Thank you very much for your answer .. Can you help me get the memory usage for the services I control.

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