Running a program in a loop

This is a discussion on Running a program in a loop within the C# Programming forums, part of the General Programming Boards category; Not quite sure if I should be posting this in the Windows Programming area or this one but I'll try ...

  1. #1
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Running a program in a loop

    Not quite sure if I should be posting this in the Windows Programming area or this one but I'll try here.

    I have 2 projects going, both executables. Project 1 uses project 2 in it's loop to get file information from specified directories. When I use it in a loop it can open hundreds of times (obviously 1 for every file is found). They all terminate after they've completed their tasks but it's absolutely horrible for the resources.
    Is there a quick code to add into my loops that will wait for that process to finish before opening a new one?

    I was thinking about using another function which verifies the file information was successfully read then runs it again but that creates the problem of errors popping up all over the place (only because that's how I have set my catches).

    For example:

    Code:
    //up here is where I get all files in folder
    //then for each file found that is in my string project2.exe is run
    foreach(string file in getfInfo)
    {
    Process.Start("project2.exe", filename)
    }
    I should almost send a screenshot of all the windows popping up and closing! It's like a little circus on my desktop!

    Thanks,

    If you need the real code I'll post it but I think it's probably not necessary.

    ---Rob Sitter
    [Professional At Novice]

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Is there a quick code to add into my loops that will wait for that process to finish before opening a new one?
    If you use CreateProcess() to create your new processes, you can then use WaitForSingleObject() to wait until that process has ended before continuing.

    EDIT: Bah, nevermind. I thought I was in the Windows Programming forum, not the C# forum
    Last edited by bithub; 12-26-2005 at 01:38 AM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Is there a quick code to add into my loops that will wait for that process to finish before opening a new one?
    Code:
    Process myProcess;
    foreach(string file in getfInfo)
    {
    	myProcess = Process.Start("project2.exe", filename)
    		while (!myProcess.HasExited)
            	myProcess.Refresh();     
      	myProcess.Close();
    }

  5. #5
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Thanks for all your help people

    Hey there, haven't logged in here in a while, noticed your posts, really appreciate them a lot!

    Thanks again,

    Rob Sitter
    [Professional at novice (almost intermediate lol)]

  6. #6
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Lightbulb One problem with that :)

    Well, it works to a degree but,

    While it's waiting for the program to exit it ups the processor usage to 100% so if I'm running a program for any length of time it's almost going to double the time it would take for it to exit.
    Is there a way to set the interval for which it checks to see if the program is still running?

    Thanks again,

    Rob Sitter

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Possibly....
    Code:
    Process myProcess;
    foreach(string file in getfInfo)
    {
    myProcess = Process.Start("project2.exe", filename)
         while (!myProcess.HasExited)
                  myProcess.Refresh();     
           Thread.Sleep(3000);  // 3 second wait    
         myProcess.Close();
    }

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    2
    great idea - thanks

    i tried to do this this way:

    Code:
    while(true){ ... }

    but this solution is better

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C keep a loop going whilst program continues on
    By fortune2k in forum C Programming
    Replies: 6
    Last Post: 03-11-2009, 08:44 AM
  2. Stop program from running twice?
    By Abda92 in forum C Programming
    Replies: 19
    Last Post: 03-17-2008, 01:35 PM
  3. running a program
    By task in forum C Programming
    Replies: 1
    Last Post: 05-31-2003, 08:01 AM
  4. Replies: 0
    Last Post: 04-27-2003, 02:04 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21