recursive function causing service to stall at start up

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pantagruel

    recursive function causing service to stall at start up

    Hi,

    I have the following:

    public static void RunBatch() {

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

    BatchDirectoryN ame = "C:\\BatchQ ";
    String[] BatchDirectory =
    Directory.GetFi les(BatchDirect oryName,"*.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 (OnDemandRunnin g) { break; }
    String BatchFile =
    BatchDirectory. GetValue(counte r).ToString();
    logthis("Batchf ile" + BatchFile, 1);
    File.Delete(Bat chFile);
    }
    System.Threadin g.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?

  • Marc Gravell

    #2
    Re: recursive function causing service to stall at start up

    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(Run Batch));
    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

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: recursive function causing service to stall at start up

      On Apr 11, 9:15 am, pantagruel <rasmussen.br.. .@gmail.comwrot e:
      Hi,
      >
      I have the following:
      >
       public static void RunBatch() {
      >
                  if (OnDemandRunnin g) {
                     //If OnDemand is Running we will do nothing
                  }
                  else{
      >
                      BatchDirectoryN ame = "C:\\BatchQ ";
                      String[] BatchDirectory =
      Directory.GetFi les(BatchDirect oryName,"*.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 (OnDemandRunnin g) { break; }
                       String BatchFile =
      BatchDirectory. GetValue(counte r).ToString();
                         logthis("Batchf ile" + BatchFile, 1);
                         File.Delete(Bat chFile);
                      }
                      System.Threadin g.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?

      Comment

      • Peter Bromberg [C# MVP]

        #4
        Re: recursive function causing service to stall at start up

        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 " <rasmussen.brya n@gmail.comwrot e in message
        news:26d0e8cc-c8d7-4b96-8e2f-8b71a62f3325@y1 8g2000pre.googl egroups.com...
        Hi,
        >
        I have the following:
        >
        public static void RunBatch() {
        >
        if (OnDemandRunnin g) {
        //If OnDemand is Running we will do nothing
        }
        else{
        >
        BatchDirectoryN ame = "C:\\BatchQ ";
        String[] BatchDirectory =
        Directory.GetFi les(BatchDirect oryName,"*.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 (OnDemandRunnin g) { break; }
        String BatchFile =
        BatchDirectory. GetValue(counte r).ToString();
        logthis("Batchf ile" + BatchFile, 1);
        File.Delete(Bat chFile);
        }
        System.Threadin g.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?
        >

        Comment

        Working...