none
Threading for asynchronous activities RRS feed

  • Question

  • I invoke a lot of asynchronous activities from my ASP.NET app. For example, when app starts, I put a file-system watcher on a folder and perform some actions when the watcher fires its events.

    Also, I subscribe to Sql Server Query Notifications (manually, not using SqlDependency) and again actions are performed when notifications are fired.

    I wonder which threads these asynchronus notifications are fired upon? Are they fired on an ASP.NET thread-pool thread, or another background thread?

    Also, the notifications for Watcher and for QN would be fired on the same thread, or they would always be on different threads?


    I always think tomorrow will have more time than today. And every today seems to pass-by faster than yesterday.
    Rahul Singla | http://www.rahulsingla.com
    Friday, October 22, 2010 5:47 AM

Answers

  • It depends on what class exactly you are using, but the thread pool is the most common case.

    The thing to really watch out for is the ASP.NET doesn't know that this stuff is running, so it considers itself free to shutdown your app pool even though you might be busy doing some things in the background.  Make sure that everything you are running in the background can tolerate this.

    > Also, the notifications for Watcher and for QN would be fired on the same thread

    Since they are from the pool, could be any thread in the pool that is free at the time of the notification (possibly the same Thread object).

    • Marked as answer by SamAgain Friday, November 5, 2010 9:53 AM
    Saturday, October 23, 2010 12:38 AM
  • > would ASP.NET allow it to complete before shutting down

    No, that's my point.

    > I think it should not allocate the same thread to both the notifications simultaneously.

    If you don't want to process two asynchronous things asynchronously, they you need to synchronize them.  For example, hold the same lock in both callbacks.

     

    • Marked as answer by SamAgain Friday, November 5, 2010 9:53 AM
    Saturday, October 23, 2010 2:36 PM

All replies

  • It depends on what class exactly you are using, but the thread pool is the most common case.

    The thing to really watch out for is the ASP.NET doesn't know that this stuff is running, so it considers itself free to shutdown your app pool even though you might be busy doing some things in the background.  Make sure that everything you are running in the background can tolerate this.

    > Also, the notifications for Watcher and for QN would be fired on the same thread

    Since they are from the pool, could be any thread in the pool that is free at the time of the notification (possibly the same Thread object).

    • Marked as answer by SamAgain Friday, November 5, 2010 9:53 AM
    Saturday, October 23, 2010 12:38 AM
  • Thanks for the reply BinaryCoder.

    >The thing to really watch out for is the ASP.NET doesn't know that this stuff is running, so it considers itself free to shutdown your app pool even though you might be busy doing some things in the background.

    If such a background thread is running, would ASP.NET allow it to complete before shutting down, or it can prematurely terminate the app in between.

    Also,

    >Since they are from the pool, could be any thread in the pool that is free at the time of the notification (possibly the same Thread object).

    My real concern is that is the 2 notifications fire in parallel, would ASP.NET choose 2 different threads (if available). I think it should not allocate the same thread to both the notifications simultaneously. However, I understand that it can allocate the same thread to the second notification after allocating the thread to one notification and allowing the first notification's processing to complete.

     


    I always think tomorrow will have more time than today. And every today seems to pass-by faster than yesterday.
    Rahul Singla | http://www.rahulsingla.com
    Saturday, October 23, 2010 6:30 AM
  • > would ASP.NET allow it to complete before shutting down

    No, that's my point.

    > I think it should not allocate the same thread to both the notifications simultaneously.

    If you don't want to process two asynchronous things asynchronously, they you need to synchronize them.  For example, hold the same lock in both callbacks.

     

    • Marked as answer by SamAgain Friday, November 5, 2010 9:53 AM
    Saturday, October 23, 2010 2:36 PM
  • If you don't want to process two asynchronous things asynchronously, they you need to synchronize them.  For example, hold the same lock in both callbacks.

    I am not concerned about them running simultaneously, they are independent. Rather, ensuring that both the asynchronous processes are not in the same thread at the same time. Thinking over it, I believe that's not possible also.

    But still, here's another question: I have SqlDependency on 2 databases: DB1 and DB2

    Suppose both get updated and fire notifications at the same instant: would the app receive them in 2 different threads (assuming 2 threads are avaialble)?
    I am just trying to understand on which threads and how these asynchronous events fire?


    I always think tomorrow will have more time than today. And every today seems to pass-by faster than yesterday.
    Rahul Singla | http://www.rahulsingla.com
    Saturday, October 23, 2010 6:43 PM