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 7194683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:25:20+00:00 2026-05-28T20:25:20+00:00

I am running some code in a while loop to ensure a program is

  • 0

I am running some code in a while loop to ensure a program is always running. If the program isn’t running it starts it, if the program isn’t there it copies it from a backup and then starts it, nothing fancy:

while (true)
{
    Process backup = new Process();
    ProcessStartInfo check = new ProcessStartInfo(file);
    if (Process.GetProcessesByName(file).Length == 0)
    {
        if(File.Exists(file))
        {
            backup.StartInfo = check;
            backup.Start();
        }
        else if (!File.Exists(file))
        {
            File.Copy(backupFile, file);
            Thread.Sleep(250);
            backup.StartInfo = check;
            backup.Start();
        }
    }
    backup.Close();
    Thread.Sleep(2000);
}

The problem is after each cycle the ram usage goes up by about 100KB which isn’t a lot I know but if this is running for an hour or so it’s going to cause big problems.

I have tried pausing it and using .Close() on the process but no joy. Any ideas are much appreciated.

  • 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-05-28T20:25:21+00:00Added an answer on May 28, 2026 at 8:25 pm
    1. Put these lines inside the if statement so they are not executed unless the process is not running:

      Process backup = new Process();
      ProcessStartInfo check = new ProcessStartInfo(file);
      
    2. Since Process implements IDisposable, you can wrap it in a using statement, as Oded suggested.

    3. You do not have an actual memory leak. When the garbage collector runs, the memory will be reclaimed. If the temporary memory usage is a problem, you could force the GC to run, but I think once you do #1 you will not have a problem with the temporary memory usage either.

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

Sidebar

Related Questions

I have some code running in my While loop: while (Time <= EndPeriod) {
I have some debugging code that if executed while running with GBD attached should
I am running some import code asynchronously from a simple WinForms app using a
I've got some code executing in a while(fscanf != EOF) loop. However, even when
In the code below, I have a while(true) loop. Consider a situation where there
I have a while loop running that is returning values from a MYSQL table.
For some reason I'm having trouble getting my program to run a while loop.
I'm running some code through FxCop and am currently looking at clearing all of
I encountered a problem when running some old code that was handed down to
I was running some dynamic programming code (trying to brute-force disprove the Collatz conjecture

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.