简体   繁体   English

在使用具有线程的Singleton更新C#应用程序中的web.config之后,IIS CPU变为bezerk

[英]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: 我有一个Web应用程序执行以下操作:

You click a button to instantiate a singleton, which creates a Thread. 单击按钮以实例化单例,从而创建一个Thread。 That Thread runs continuously doing some HTTP requests to gather some data. 该Thread持续运行一些HTTP请求来收集一些数据。 You can click a stop button that calls the Abort() method on the thread and the application stops making HTTP requests. 您可以单击在线程上调用Abort()方法的停止按钮,并且应用程序停止发出HTTP请求。 When I start/stop it manually, everything works fine. 当我手动启动/停止它时,一切正常。

My problem occurs when ever I "touch" web.config. 当我“触摸”web.config时,我的问题就出现了。 The CPU (w3wp.exe process) spikes and the website stops responding. CPU(w3wp.exe进程)激增,网站停止响应。 Does anybody know why this is happening? 有人知道为什么会这样吗? Shouldn't an update to web.config reset everything? 不应该更新web.config重置一切?

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 == START);
}

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. 当您的应用程序域重置时,您将获得一个ThreadAbort异常,该异常将在此处捕获并触发递归调用,这将触发另一个异常,以及另一个递归调用。 It's turtles all the way down! 它一直是乌龟!

Yes, making any change in web .config resets the application, and asp.net re-build the application. 是的,在web .config中进行任何更改都会重置应用程序,并且asp.net会重新构建应用程序。

Same is true for some other files like files under Bin and App_Code folders. 对于Bin和App_Code文件夹下的文件等其他文件也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM