Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8459127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:15:37+00:00 2026-06-10T13:15:37+00:00

I have this basic code that will check for notepad running every minute. namespace

  • 0

I have this basic code that will check for notepad running every minute.

namespace Watcher
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; ; i--)
            {
                foreach (Process clsProcess in Process.GetProcesses())
                {
                    if (clsProcess.ProcessName.Contains("notepad"))
                    {
                        Console.WriteLine("True");
                    }
                    Console.WriteLine("NFalse");
                }
                Thread.Sleep(10000);
            }
        }
    }
}

The problem is that it returns “NFalse” for every running process (It will print 100 of them for example). How can I just make this print once to show that the process is not running?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:15:38+00:00Added an answer on June 10, 2026 at 1:15 pm

    Refactor your code.

    You’re doing too much in one method. Put your code that checks to see if notepad is running into a separate method:

    static bool CheckIfProcessIsRunning(string nameSubstring)
    {
        foreach (Process clsProcess in Process.GetProcesses())
        {
            if (clsProcess.ProcessName.Contains(nameSubstring))
            {
                return true;
            }
        }
        return false;
    }
    

    You could simplify this further using LINQ:

    static bool CheckIfProcessIsRunning(string nameSubstring)
    {
        return Process.GetProcesses().Any(p => p.ProcessName.Contains(nameSubstring));
    }
    

    Once you have written this method, all that remains is to call it and print the right message depending on whether it returns true or false.

    while (true)
    {
        string message = CheckIfProcessIsRunning("notepad") ? "True" : "NFalse";
        Console.WriteLine(message);
        Thread.Sleep(10000);
    }
    

    Now instead of one long method with complex logic, you have two very simple methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code below. This code is a basic push/pop stack class that
I have been trying to create a Ruby program that will be running online
So I have this page that has some basic custom tabs: http://demo.unlockedmanagement.com/users/view/2 * NOTE
Basic C# syntax question: So I have this class public class BrandQuery<T> : Query<T>
So I have this stored proc that will not get created when I run
The basic idea is this: I have a form that generates form fields dynamically
This program will have an infinite canvas (ie as long as the user scrolls,
Hi I have this basic random quote script, but I would like it to
I have a js.erb with this basic content: $(#currencies).html(<%= options_for_select([['Dollar', '$']])%>); And the html
I have this table, which can be seen as a basic custom gantt chart:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.