char m
char m

Reputation: 8336

what happens to ASP.NET exceptions from created non-threadpool thread?

In ASP.NET web application a worker thread creates a non-threadpool thread like the below:

private static bool _refreshInProcess = false;
public delegate void Refresher();
private static Thread _refresher;

private static void CreateAndStartRefreshThread(Refresher refresh)
{
    _refresher = new Thread(new ThreadStart(refresh));
    _refresher.Start();
}

private static void Refresh()
{
    LoadAllSystemData();
}

static public void CheckStatus()
{
    DateTime timeStamp = DateTime.Now;
    TimeSpan span = timeStamp.Subtract(_cacheTimeStamp);
    if (span.Hours >= 24)
    {
        if (Monitor.TryEnter(_cacheLock))
        {
            try
            {
                if (!_refreshInProcess)
                {
                    _refreshInProcess = true;
                    CreateAndStartRefreshThread(Refresh);
                }
            }
            finally
            {
                Monitor.Exit(_cacheLock);
            }
        }
    }
}

static public void LoadAllSystemData()
{
    try
    {
        if (!Database.Connected)
        {
            if (!OpenDatabase())
              throw new Exception("Unable to (re)connect to database");  
        }
        SystemData newData = new SystemData();
        LoadTables(ref newData);
        LoadClasses(ref newData);
        LoadAllSysDescrs(ref newData);
        _allData = newData;
        _cacheTimeStamp = DateTime.Now; // only place where timestamp is updtd 
    }
    finally
    {
        _refreshInProcess = false;
    }
}

and LoadAllSystemData() is also called elsewhere from the same lock-guarded section as CheckStatus(). Both calls are in their try-blocks that also have catch-block.

Now my questions are

  1. If LoadAllSystemData throws an exception when it's called from a non-threadpool thread in method Refresh, what will happen? Nobody can catch it.

  2. What happens when it's done 1000 times? Are these exceptions stored somewhere and thus stress the system and ultimately crash it due to memory exhaustion or something?

  3. Is there a good solution to catch them without waiting in the creating thread pool thread for the created thread to finish?

Thanks a lot!

Upvotes: 1

Views: 324

Answers (1)

Vitaliy Liptchinsky
Vitaliy Liptchinsky

Reputation: 5299

  1. If exception is raised in the background non-threadpool thread and eventually it is not handled by any catch block, it is propagated up until it reaches beginning of the stack. Then thread finishes its execution - exception is not stored anywhere. Then the thread is dropped as well as its stack containing reference to the exception.
  2. Exception throwing is expensive in .NET so if you anticipate 1000 times it to occur, probably it is not an exception but just a part of your application flow and you should not raise an exception here.
  3. If you want to catch the exceptions from background activity, as an option you can use delegate's BeginInvoke / EndInvoke methods. If there is any exception in the background operation, it will be delivered in the EndInvoke call.

Upvotes: 1

Related Questions