471,337 Members | 1,198 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

recursive function causing service to stall at start up

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?

Apr 11 '08 #1
3 1812
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
Apr 11 '08 #2
On Apr 11, 9:15*am, pantagruel <rasmussen.br...@gmail.comwrote:
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?
Apr 11 '08 #3
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
"pantagruel" <ra*************@gmail.comwrote in message
news:26**********************************@y18g2000 pre.googlegroups.com...
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?
Apr 11 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Stian Søiland | last post: by
10 posts views Thread by Steve Goldman | last post: by
2 posts views Thread by | last post: by
8 posts views Thread by Ryan Stewart | last post: by
4 posts views Thread by Jerry Camel | last post: by
5 posts views Thread by Adrian | last post: by
9 posts views Thread by Csaba Gabor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.