recursive function causing service to stall at start up

P

pantagruel

Hi,

I have the following:

public static void RunBatch() {

if (OnDemandRunning) {
//If OnDemand is Running we will do nothing
}
else{

BatchDirectoryName = "C:\\BatchQ";
String[] BatchDirectory =
Directory.GetFiles(BatchDirectoryName,"*.xml");

for (int counter = 0; counter < BatchDirectory.Length;
counter++) {
///<remarks>
/// need to break if an OnDemand event happens.
///</remarks>
///
//TODO: Implement actual event handling etc. instead of
the fake event handling doing now.
if (OnDemandRunning) { break; }
String BatchFile =
BatchDirectory.GetValue(counter).ToString();
logthis("Batchfile" + BatchFile, 1);
File.Delete(BatchFile);
}
System.Threading.Thread.Sleep(5000);
RunBatch();



}

}

Now I'm not sure if any of the rest of the code is a problem but the
presence of RunBatch(); in RunBatch() is causing it to stall at load.
I've set the Sleep to be really high to avoid any problem like that
but it seems like that does not work. How does one get around that?
 
M

Marc Gravell

The service-start method needs to exit promptly; in most cases, the
"start" to a service simply fires up a new thread to run (in this
case) RunBatch - i.e.

protected override void OnStart(string[] args)
{
Thread thread = new Thread(new ThreadStart(RunBatch));
thread.Name = "RunBatch";
thread.Start();
}

Also - why does this need to be recursive? It is surenly guaranteed to
blow the stack eventually... why not just "do/while" or similar?

Marc
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,

I have the following:

 public static void RunBatch() {

            if (OnDemandRunning) {
               //If OnDemand is Running we will do nothing
            }
            else{

                BatchDirectoryName = "C:\\BatchQ";
                String[] BatchDirectory =
Directory.GetFiles(BatchDirectoryName,"*.xml");

            for (int counter = 0; counter < BatchDirectory.Length;
counter++) {
                    ///<remarks>
                    /// need to break if an OnDemand event happens.
                    ///</remarks>
                    ///
            //TODO: Implement actual event handling etc. instead of
the fake event handling doing now.
                    if (OnDemandRunning) { break; }
                 String BatchFile =
BatchDirectory.GetValue(counter).ToString();
                   logthis("Batchfile" + BatchFile, 1);
                   File.Delete(BatchFile);
                }
                System.Threading.Thread.Sleep(5000);
               RunBatch();

            }

        }

Now I'm not sure if any of the rest of the code is a problem but the
presence of RunBatch(); in RunBatch() is causing it to stall at load.
I've set the Sleep to be really high to avoid any problem like that
but it seems like that does not work. How does one get around that?

the OnStart method is intended to return at once.
Create a thread and do your processing there.

Question, what you do when the batch is completed? how the process
gets the next batch?
 
P

Peter Bromberg [C# MVP]

As Marc indicated, you want to do this kind of operation on a background
thread so your OnStart method returns quickly. Also, instead of Sleep you
probably want to use a timer.
-Peter
 
Top