locked
Main() execution loop for Server or Web App? RRS feed

  • Question

  • User861468555 posted

    I am using multiple threads in my .net web application.  Certain functions may need to execute at any arbitrary time.  However, I do not need these (and the entire application) to be fully asynchronous, as there is an acceptable tolerance in this execution time.  Here is a rough example of what I need to do:


    main()

    {

        loop

       {

           if (flagA) then doTaskA();

           if (flagB) then doTaskB();

       }

    }

    ...where flagA and flagB are set via asynchronous timers in critical section.  The actual execution of the requested tasks would thereafter run in series.

    This is very easy to do in C/C++, etc.  But I cannot find any access to such control in .NET, specifically in the web server/application architecture.  It may be that it doesn't exist; however, I am assuming that the server must do something of this sort as it manages applications.  I would like to hook into that somehow; even if via some application function that is called regularly during this loop.

    Wednesday, June 30, 2010 4:20 PM

Answers

  • User-389939489 posted

    main()
    {
        loop
       {
          normalServerTasks();
          app.UpdateSomething();  // may require application start, etc.
       }
    }

    There is a logical problem with that, namely that you cannot get above the 'normalServerTask' level. What you can do rather looks something like this:

    normalServerLifeCycle()
    {
        (normalServerTasks();)
        app_OnStart -> Start UpdateSomething(); (start the timer)
            Do UpdateSomething(); (within the timer worker process)
        app_OnEnd -> Stop UpdateSomething(); (stop and cleanup)
    }

    That said, I think I have provided you with an answer: maybe not everything is clear? If so, feel free to get into the details.

    -LV

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 5, 2010 3:01 PM

All replies

  • User-2004844803 posted

    Hi,

    I dont think its a good idea to use your web application for this since they are just not designed for it. My suggestion is that you try to break your application into separate parts running on different hosts. You say you have some timers setting the flags and then you want your loop to starts your tasks. Cant you instead let your timers post a message to a message que (or make a WCF call or any other way...) and then have a "TaskExecutorService" running as a service picking up your messages (or as a WCF service for your WCF calls or some thing else...) on either the same machine or some other machine?


    Thursday, July 1, 2010 2:48 AM
  • User-952121411 posted

    Certain functions may need to execute at any arbitrary time.  However, I do not need these (and the entire application) to be fully asynchronous
     

    I would go a different route by designing a Windows Service that runs on interval (whatever time you want) to check for the 'flags' or conditions (flagA and flagB in your code) and then launch off the appropriate code (doTaskA() and doTaskB()).  Once you start the Windows Service it runs uninterrupted and is perfect for these types of long running background tasks that do not or should not require user interaction; much preferred over an ASP.NET app which is intended much more for a live client server interaction with 'Session State' and things of that nature that can present road bumps.

    Even if these tasks are directly correlated to a user's ASP.NET web session, you can still offload the polling and task process to an external service.  As long as your ASP.NET app sets the flag, the service will take over from there.

    When the Windows Service is started your timers will begin as well.  They will fire on interval and you can call a database, WCF service, whatever to get the status of the flags.  Then if 'true' you can fire of the additional tasks.  It is really quite trivial to set up once you comprehend the pieces.  Take a look to the following link which should help explaining this process:

    Running a Periodic Process in .NET using a Windows Service: 

    http://allen-conway-dotnet.blogspot.com/2009/12/running-periodic-process-in-net-using.html

    Hope this helps! Smile

    Thursday, July 1, 2010 10:06 AM
  • User-389939489 posted

    I would like to hook into that somehow; even if via some application function that is called regularly during this loop.

    You can start a timer from Application_OnStart, and clean-up in Application_OnEnd. (Look for the System.Timers.Timer class, though there are other options.)

    Moreover, you can assign the web app to a specific application pool, then you can set options on the application pool for recycling, performance, etc. Namely, you have settings to prevent the worker process to shutdown when the app is idle, and so on.

    Using a Windows Service is a more complex approach, from both a development and a deployment point of view: I'd suggest you do it only if you need a higher level of functionality, otherwise it is overkill (despite what you'll read around the web).

    HTH,

    -LV

    Thursday, July 1, 2010 12:23 PM
  • User861468555 posted

    Thanks.

    Though I honestly appreciate suggestions for alternatives, I would most appreciate an answer to the question.

    Thursday, July 1, 2010 3:07 PM
  • User861468555 posted

    It is my fault for diverging into my particular application.  The following is a simplified version of essentially the same question.  I need to know how to do this:


    main()

    {

        loop

       {

          normalServerTasks();

          app.UpdateSomething();  // may require application start, etc.

       }

    }

    Thursday, July 1, 2010 3:29 PM
  • User-2004844803 posted

    You can start a timer from Application_OnStart, and clean-up in Application_OnEnd. (Look for the System.Timers.Timer class, though there are other options.)

    Moreover, you can assign the web app to a specific application pool, then you can set options on the application pool for recycling, performance, etc. Namely, you have settings to prevent the worker process to shutdown when the app is idle, and so on.

    you could do llike this but keep in mind that a running timer is not enough to keep your application from being recycled. If you on the other hand chose to disable recycling you lose a great feature for the rest of your application. In my opinion its not complex approach using a service for these parts, its better to put that extra effort into building the service then it is to try to squeeze it into your web application.

    Friday, July 2, 2010 2:58 AM
  • User-389939489 posted

    @toas1:

    > keep in mind that a running timer is not enough to keep your application from being recycled.

    Right.

    > If you on the other hand chose to disable recycling you lose a great feature for the rest of your application.

    Right. The point is, you *don't* need to disable recycling: you have hooked your timer to the Application_OnStart and Application_OnEnd events, so the timer restarts too.

    > In my opinion its not complex approach using a service for these parts, its better to put that extra effort into building the service then it is to try to squeeze it into your web application.

    There is just nothing to squeeze: a win service is an additional component to the system.

    -LV

    Friday, July 2, 2010 6:11 AM
  • User861468555 posted

    Have you guys read the question?

    Saturday, July 3, 2010 4:35 AM
  • User-2004844803 posted

    Actually,

    I cant see no real question (there is not a single question mark anywhere) in your post but I asumed you wanted help on how to solve your problem. But please, just let us know if we are on the wrong track...

    Saturday, July 3, 2010 12:34 PM
  • User861468555 posted

    I have stated what I am attempting to do quite clearly, twice.  Please do not waste my time or that of others.  If you insist on flaunting personal inadequacies instead, I will point out that there is indeed a "question mark" in my post, and that when patronizing others it is a good idea to have a solid command of the English language and avoid the use of double negatives "I can't see no real question" and misspellings "asumed" (which, in fact, have been directly linked to general IQ).

    If you cannot answer the question, then simply don't.

    Saturday, July 3, 2010 2:33 PM
  • User-2004844803 posted

    sorry for trying to help you. but don't worry, I can promise you that it won't happen again :)


    Saturday, July 3, 2010 2:44 PM
  • User861468555 posted

    Thanks.

    Saturday, July 3, 2010 4:12 PM
  • User-2004844803 posted

    no prob. But I guess people are lining up to help an ungrateful .... like you. Good luck :)

    Sunday, July 4, 2010 3:36 AM
  • User861468555 posted

    They have done what I asked - answer the question, or don't.  What are you doing?

    You may find it fruitful to pander to your insecurities by clicking back through here, but I view it as a waste of time.

    Learn to separate your feelings from facts and logic, and please take your emotions to a more suitable arena.

    Sunday, July 4, 2010 1:57 PM
  • User-952121411 posted

    Slimfinger -

    I would be interested on your feedback from my 1st response to you.  The difficulty with your question is trying to get a square peg in a round hole deal.  Your pseudo code examples like you mentioned that were easy to do in C/C++ are in the mindset of thick client application programming (i.e. a Windows Application).  The problem at hand is trying to accomplish the same end result and task in a stateless architecture like the web and ASP.NET with the tools provided.  Parsing out the application into services that can handle long running update tasks (i.e. A Windows Service) is a way you can inject the long running processes back into a web architecture.

    If you are looking for someone to drop a few lines of code here that solves the original question, I don't think that will happen.  You certainly have access to the System.Threading namespace once on the server from a client call, but once your server finishes processing that call there is no state between that call and the next one (without using some persistence mechanism like Session variables, etc.).  So multithreading is possible, but only per call made to the server.  To make tasks like this run indefinitely, you need to move it out to something that can run for long periods of time and not have to worry about the stateless environment.  You could also shift focus to something like AJAX or JS timers that run client side and then fire off server-side code, but I only use this approach for small client specific needs since it may be unreliable (browser crash, etc.)

    I know this may not be the answer you were looking for, but in ASP.NET these are some of the things we have to do to mimic or emulate thick client programming models that have no issue running that type of code.

    Monday, July 5, 2010 10:18 AM
  • User-389939489 posted

    main()
    {
        loop
       {
          normalServerTasks();
          app.UpdateSomething();  // may require application start, etc.
       }
    }

    There is a logical problem with that, namely that you cannot get above the 'normalServerTask' level. What you can do rather looks something like this:

    normalServerLifeCycle()
    {
        (normalServerTasks();)
        app_OnStart -> Start UpdateSomething(); (start the timer)
            Do UpdateSomething(); (within the timer worker process)
        app_OnEnd -> Stop UpdateSomething(); (stop and cleanup)
    }

    That said, I think I have provided you with an answer: maybe not everything is clear? If so, feel free to get into the details.

    -LV

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 5, 2010 3:01 PM
  • User-1573135047 posted

    Why not just write a simple console app that gets kicked off by the Windows Task Scheduler?


    Friday, July 16, 2010 1:49 PM
  • User-2004844803 posted

    Why not just write a simple console app that gets kicked off by the Windows Task Scheduler?

    Laughing

    Saturday, July 17, 2010 1:13 AM