2

EDIT: Thanks everyone for providing their answers here is the project completed.


https://github.com/0xyg3n/ProcessDaemon/


If someone comes up with multi thread solution that might work better i think.


I am new in C# and i would like to ask what is the best way to create an intentional infinite loop that does not destroy your cpu usage... More like as a daemon.

Concept of Software is : Create a software that is able to determine if a specific process is running in order to execute a block of code break the loop and make the loop again. I know how to scan the processes and make the loop etc etc but i want something lightweight. C# by default is running as single thread , maybe if i did the while(true) as multi threaded that would solve my problem???

Thanks in advance!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace ProcessDaemon
{
    class Program
    {
        static void Main(string[] args)
        {

            void CP()
            {
                Process[] pname = Process.GetProcessesByName("notepad");
                if (pname.Length == 0)
                {
                    //if not running
                }
                else
                {
                    //if running
                    //break the code
                    //retry loop
                }
            }


            void cronCP()
            {
                while(true)
                {
                    CP();
                }
            }

            cronCP(); //scan the processes infinitely.
        }
    }
}
0xyg3n
  • 33
  • 10
  • 4
    All you need is a `Timer`. – Nick Jun 08 '20 at 09:37
  • Well a timer could do what i need but i don't want a timer for that because i want for it to be instant responsive. – 0xyg3n Jun 08 '20 at 09:49
  • 1
    The solution you accepted isn't instantly responsive either. It will also do its work every 1 sec. – Nick Jun 08 '20 at 17:16
  • Yes but the cpu usage is really not that bad ! i do believe though that the best way would be with multi threads. i just don't know how to. – 0xyg3n Jun 09 '20 at 07:22

1 Answers1

2

You can turn your code into async code and then add a Task.Delay(xx) to release the process while awaiting

namespace ProcessDaemon
{
    class Program
    {
        static async Task Main(string[] args)
        {

            void CP()
            {
                Process[] pname = Process.GetProcessesByName("notepad");
                if (pname.Length == 0)
                {
                    //if not running
                }
                else
                {
                    //if running
                    //break the code
                    //retry loop
                }
            }


            async Task cronCP()
            {
                while (true)
                {
                    await Task.Delay(1000);
                    CP();
                }
            }

            await cronCP(); //scan the processes infinitely.
        }
    }
}
Thibaut
  • 342
  • 1
  • 7
  • Will this make the process lightweight and is this a proper way to make a loop as a deamon?? Like in the near future i might use that for different things, like as an event listener! Wait for something to happen and then trigger a specific action. But yes the question is if it is the right way to do it like that? – 0xyg3n Jun 08 '20 at 09:48
  • 1
    Async is good and I thought that the Process itself might be an awaitable handle or similar, but then I found this answer https://stackoverflow.com/a/470288/2048821 - seems as though the Process class can raise events, so it was made with this in mind - just get the process once and then respond to it's event. Just need a way to stop the main thread from closing now. You could maybe do that with a ManualResetEventSlim. – Mark Rabjohn Jun 08 '20 at 09:55