开发者

IIS CPU goes bezerk after updating web.config in a C# application with a Singleton with a thread

I have a web application that does the following:

You click a button to instantiate a singleton, which creates a Thread. That Thread runs continuously doing some HTTP requests to gather some data. You can click a stop button that calls the Abort() method on the thread and the application stops making HTTP requests. When I start/stop it manually, everything works fine.

My problem occurs when ever I "touch" web.config. The CPU (w3wp.exe process) spikes and the website stops responding. Does anybody know why this is happening? Shouldn't an update to web.config reset everything?

Sample code is below:

private static MyProcessor mp = null;
private Thread theThread = null;
private string status = STOP;
public static string STOP = "Stopped";
public static string START = "Started";

private MyProcessor()
{}

public static MyProcessor getInstance()
{
    if (mp == null)
    {
        mp = new MyProcessor();
    }
    return mp;
}

public void Start()
{
    if (this.status == START)
        return;

    this.theThread = new Thread(new ThreadStart(this.StartThread));
    this.theThread.Start();
    this.status = START;
}

public void Stop()
{
    if (this.theThread != null)
        this.theThread.Abort();
    this.status = STOP;
}

private void StartThread()
{
    do
    {
        try
        {
            //do some work with HTTP requests
            Thread.Sleep(1000 * 2);
        }
        catch (Exception e)
        {
            //retry - work forever
            this.StartThread();
        }
    } while (this.status == STAR开发者_运维技巧T);
}


I suspect this is the problem:

private void StartThread()
{
    do
    {
        try
        {
            //do some work with HTTP requests
            Thread.Sleep(1000 * 2);
        }
        catch (Exception e)
        {
            //The recursive call here is suspect
            //at the very least find a way to prevent infinite recursion
            //--or rethink this strategy
            this.StartThread();
        }
    } while (this.status == START);
}

When your app domain resets, you'll get a ThreadAbort exception which will be caught here and trigger a recursive call, which will hit another exception, and another recursive call. It's turtles all the way down!


Yes, making any change in web .config resets the application, and asp.net re-build the application.

Same is true for some other files like files under Bin and App_Code folders.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜