views:

41

answers:

1

Hi, In ASP.NET web application a worker thread creates a non-threadpool thread like 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 same lock-guarded section as CheckStatus. Both calls are in their try-bolcks that also have catch-block.

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

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

  2. Is there good solution to catch them without waiting in the creating threadpool thread for the created thread to finish?

Thanks a lot! -Matti

A: 
  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.
Vitaliy Liptchinsky
thanks a lot! don't want to use Begin/EndInvoke since it uses threadpool thread and calling EndInvoke stops the thread that created the thread until it finishes.
matti
BeginInvoke returns IAsyncResult object. You can query this object's property IsCompleted if you want to prevent your request thread from blocking.If you want to avoid ThreadPool at all, then the only way to go is Thread class and manual thread synchronization
Vitaliy Liptchinsky